<!DOCTYPE HTML>
<html lang="Chinese">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="java进阶, 博客">
    <meta name="description" content="In me the tiger sniffs the rose.">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>java进阶 | 凡诚</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
    <script src="/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.4.2"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size:cover;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/comment_bg.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">凡诚</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>主页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>文章</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于我</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/about">
          
          <i class="fas fa-user-circle" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>我的信息</span>
        </a>
      </li>
      
      <li>
        <a href="/contact">
          
          <i class="fas fa-comments" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>留言板</span>
        </a>
      </li>
      
      <li>
        <a href="/friends">
          
          <i class="fas fa-address-book" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>友链</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="Search" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/comment_bg.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">凡诚</div>
        <div class="logo-desc">
            
            In me the tiger sniffs the rose.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			主页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			文章
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-user-circle"></i>
			
			关于我
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/about " style="margin-left:75px">
				  
				   <i class="fa fas fa-user-circle" style="position: absolute;left:50px" ></i>
			      
		          <span>我的信息</span>
                  </a>
                </li>
              
                <li>

                  <a href="/contact " style="margin-left:75px">
				  
				   <i class="fa fas fa-comments" style="position: absolute;left:50px" ></i>
			      
		          <span>留言板</span>
                  </a>
                </li>
              
                <li>

                  <a href="/friends " style="margin-left:75px">
				  
				   <i class="fa fas fa-address-book" style="position: absolute;left:50px" ></i>
			      
		          <span>友链</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/7.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">java进阶</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/java/">
                                <span class="chip bg-color">java</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E5%90%8E%E7%AB%AF%E6%8A%80%E6%9C%AF/" class="post-category">
                                后端技术
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>Publish Date:&nbsp;&nbsp;
                    2022-03-11
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>Update Date:&nbsp;&nbsp;
                    2022-11-10
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>Word Count:&nbsp;&nbsp;
                    24.9k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>Read Times:&nbsp;&nbsp;
                    101 Min
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>Read Count:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h3 id="42-Collections工具类"><a href="#42-Collections工具类" class="headerlink" title="42. Collections工具类"></a>42. Collections工具类</h3><p>  Collections是一个操作Set,List和Map等集合的工具类</p>
<p>  Collections中提供了一些列静态的和方法对集合元素进行排序，查找，修改等操作。</p>
<h4 id="42-1-排序操作"><a href="#42-1-排序操作" class="headerlink" title="42.1 排序操作"></a>42.1 排序操作</h4><p>  排序操作均为static方法。</p>
<ol>
<li>reverse(List): 反转List中元素的顺序。</li>
<li>shuffle(List):对List集合元素进行随机排序。</li>
<li>sort(List):根据元素的自然顺序对只当List集合按照升序排序。</li>
<li>sort(List,Comparator);根据指定的Conmparator产生的顺序对List集合元素进行排序。</li>
<li>swap(List,int,int); 将指定List集合中的i处元素和j处元素进行交换。</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span> li <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
li<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
li<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
li<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


<h4 id="42-2-查找，替换"><a href="#42-2-查找，替换" class="headerlink" title="42.2 查找，替换"></a>42.2 查找，替换</h4><ol>
<li>Object max(Collection); 根据元素的自然顺序，返回给定结合中最大的元素。</li>
<li>Object max(Collection,Comparator);根据Comparator指定的顺序，返回给定集合中的最大元素。</li>
<li>Object min(Collection); 根据元素的自然顺序，返回给定结合中最小的元素。</li>
<li>Object min(Collection,Comparator);根据Comparator指定的顺序，返回给定集合中的最小元素。</li>
<li>int frequency(Collection,Object); 返回指定集合中指定元素的出现次数。</li>
<li>void copy(List dest,List src):将src中的内容赋值到dest中。</li>
<li>boolean replaceAll(List list,Object oldVal,Object newVal);使用新值替换List对象的所有旧值。</li>
</ol>
<h3 id="43-泛型"><a href="#43-泛型" class="headerlink" title="43. 泛型"></a>43. 泛型</h3><p> 泛型又称参数化类型，是jdk5.0中出现的新特性，解决数据类型的安全性问题。</p>
<p>  在类声明或者实例化是只要指定需要的具体的类型即可。该类型会在定义对象时指定，在编译期间确定。</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ArrayList</span><span class="token operator">&lt;</span><span class="token class-name">String</span>  strings <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator">&lt;</span><span class="token class-name">String</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>  java泛型可以保证程序在编译时没有发生警告，运行时就不会产ClassCastException异常，同时代码更加简洁，健壮。</p>
<p>  泛型的作用：可以在类声明时通过一个&lt; 表示类中某个属性的类型，或者是某个方法的返回值的类型，或者是参数类型。</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> doubao<span class="token operator">&lt;</span><span class="token class-name">E</span> <span class="token punctuation">&#123;</span>
<span class="token class-name">E</span> id<span class="token punctuation">;</span> <span class="token comment">//E 表示数据类型，该数据类型时在定义对象时指定，在编译期间会确定E的类型。</span>
<span class="token class-name">String</span> name<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token class-name">E</span> <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  <span class="token keyword">return</span> id<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>  泛型 在编译时，检查添加元素的类型，提高了安全性。减少了类型转换的次数，提高了效率。不再提示编译警告。</p>
<h4 id="43-1-泛型使用"><a href="#43-1-泛型使用" class="headerlink" title="43.1 泛型使用:"></a>43.1 泛型使用:</h4><p>  泛型语法:</p>
<p>  interface 接口&lt;T {} 和class 类 &lt;K,V {}</p>
<ul>
<li>其中T,K,V不代表值,而是表示类型.</li>
<li>任意字母都可以,常用T表示,是Type的缩写.</li>
</ul>
<p>  泛型实例化:</p>
<p>  在类名后指定参数类型的值(类型):</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ArrayList</span><span class="token operator">&lt;</span><span class="token class-name">String</span>  strings <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator">&lt;</span><span class="token class-name">String</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Iterator</span><span class="token operator">&lt;</span><span class="token class-name">Customer</span>  iterator <span class="token operator">=</span> customers<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>  细节:</p>
<ol>
<li><p>interface List&lt;T {}, public class HashSet&lt;E {}等</p>
<p>T,E只能是引用类型.</p>
</li>
<li><p>在泛型指定具体类型后,可以传入该类型或者其子类类型.</p>
</li>
<li><p>泛型的使用形式和简写形式:</p>
</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span><span class="token operator">&lt;</span><span class="token class-name">Integer</span>  strings <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">List</span><span class="token operator">&lt;</span><span class="token class-name">Integer</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//简写形式 编译器会进行类型推断</span>
<span class="token class-name">List</span><span class="token operator">&lt;</span><span class="token class-name">Integer</span>  strings <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">List</span><span class="token operator">&lt;</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<ul>
<li>如果不指定具体的泛型类型时,默认是object类型.</li>
</ul>
<h4 id="43-2-自定义泛型类"><a href="#43-2-自定义泛型类" class="headerlink" title="43.2 自定义泛型类"></a>43.2 自定义泛型类</h4><p>  基础语法；</p>
<p>  class 类名&lt;T,R… { 成员 } </p>
<p>  细节：</p>
<ol>
<li>普通成员可以使用泛型(属性，方法).</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Genericclass</span><span class="token operator">&lt;</span><span class="token class-name">T</span><span class="token punctuation">,</span><span class="token class-name">R</span><span class="token punctuation">,</span><span class="token class-name">M</span> <span class="token punctuation">&#123;</span>
 <span class="token class-name">String</span> name<span class="token punctuation">;</span>
 <span class="token class-name">R</span> r<span class="token punctuation">;</span>                    <span class="token comment">//属性使用泛型</span>
 <span class="token class-name">M</span> m<span class="token punctuation">;</span>
 <span class="token class-name">T</span> t<span class="token punctuation">;</span>

	<span class="token keyword">public</span> <span class="token class-name">Genericclass</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token class-name">R</span> r<span class="token punctuation">,</span> <span class="token class-name">M</span> m<span class="token punctuation">,</span> <span class="token class-name">T</span> t<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
     <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
     <span class="token keyword">this</span><span class="token punctuation">.</span>r <span class="token operator">=</span> r<span class="token punctuation">;</span>
     <span class="token keyword">this</span><span class="token punctuation">.</span>m <span class="token operator">=</span> m<span class="token punctuation">;</span>
     <span class="token keyword">this</span><span class="token punctuation">.</span>t <span class="token operator">=</span> t<span class="token punctuation">;</span>
 <span class="token punctuation">&#125;</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">getR</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>    <span class="token comment">//方法使用泛型</span>
     <span class="token keyword">return</span> r<span class="token punctuation">;</span>
 <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ol start="2">
<li>==使用泛型的数组，不能初始化。==</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//数组在创建的时候不能初始化，只能定义</span>
<span class="token class-name">T</span><span class="token punctuation">[</span><span class="token punctuation">]</span> ts<span class="token punctuation">;</span>
<span class="token comment">//  T[] te = new T[3]  无法初始化数组。因为不能确定创建空间的类型以及大小。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<ol start="3">
<li><p>泛型标识符可以有多个。</p>
</li>
<li><p>==静态方法中和静态属性不能使用类的泛型==。</p>
</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//静态方法不能使用类的泛型，因为静态方法可能在类实例化之前就被调用</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<ol start="5">
<li><p>泛型类的类型，是在创建对象时确定的(因为创建对象时，需要指定确定类型)</p>
</li>
<li><p>如果创建对象时，没有指定类型，默认为Object类型。</p>
</li>
</ol>
<h4 id="43-3-自定义泛型接口"><a href="#43-3-自定义泛型接口" class="headerlink" title="43.3 自定义泛型接口"></a>43.3 自定义泛型接口</h4><p>  基础语法；</p>
<p>  interface 类名&lt;T,R… { 成员 } </p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">interface</span>  <span class="token class-name">GenericInt</span><span class="token operator">&lt;</span><span class="token class-name">U</span><span class="token punctuation">,</span><span class="token class-name">R</span> <span class="token punctuation">&#123;</span>
<span class="token class-name">R</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">U</span> u<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">default</span>  <span class="token class-name">R</span> <span class="token function">method</span><span class="token punctuation">(</span><span class="token class-name">U</span> u<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">class</span> <span class="token class-name">Genclass</span> <span class="token keyword">implements</span> <span class="token class-name">GenericInt</span><span class="token operator">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">Integer</span> <span class="token punctuation">&#123;</span>

<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token class-name">Integer</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">String</span> s<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">setU</span><span class="token punctuation">(</span><span class="token class-name">String</span> s<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token class-name">Integer</span> <span class="token function">method</span><span class="token punctuation">(</span><span class="token class-name">String</span> s<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>  细节：</p>
<ol>
<li>接口中，静态成员也不能使用泛型。(接口中的属性都是静态属性)</li>
<li>泛型接口的类型，在继承接口或者实现接口时确定。</li>
<li>==没有指定类型，默认为Object。==</li>
</ol>
<h4 id="43-4-自定义泛型方法"><a href="#43-4-自定义泛型方法" class="headerlink" title="43.4 自定义泛型方法"></a>43.4 自定义泛型方法</h4><p>  基本语法：</p>
<p>  修饰符 &lt;T,R…  返回类型 方法名(参数列表){}</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>   <span class="token comment">//使用泛型的普通方法</span>

<span class="token keyword">public</span> <span class="token operator">&lt;</span><span class="token class-name">T</span><span class="token punctuation">,</span><span class="token class-name">R</span>  <span class="token keyword">void</span> <span class="token function">fly</span><span class="token punctuation">(</span><span class="token class-name">T</span> t<span class="token punctuation">,</span><span class="token class-name">R</span> r<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>  <span class="token comment">//泛型方法</span>

<span class="token comment">//调用方法：</span>
<span class="token class-name">Car</span> car <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Car</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
car<span class="token punctuation">.</span><span class="token function">fly</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//当调用方法时，传入参数，编译器会确定类型</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>  细节：</p>
<ul>
<li>泛型方法可以定义在普通类中，也可以定义在泛型类中。</li>
<li>当泛型方法被调用时，类型会确定。</li>
<li>public void 方法名(E e){} 修饰符后没有&lt;T,R,…  该方法不是泛型方法，而是使用了泛型。</li>
<li>泛型方法可以使用类声明的泛型，也可以使用自己声明的泛型。</li>
</ul>
<h4 id="43-5-泛型的继承和通配符"><a href="#43-5-泛型的继承和通配符" class="headerlink" title="43.5 泛型的继承和通配符"></a>43.5 泛型的继承和通配符</h4><p>  ==泛型不具备继承性==</p>
<p> <code> &lt;?  : 支持任意类型函数。</code></p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//List&lt;父类  strings = new List&lt;子类 (); 不被允许</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p> <code> &lt;? extends A  :支持A类以及A类的子类，规定泛型的上限。</code></p>
<p><code>  &lt;? super A : 支持A类以及A类的父类，不限于直接父类，规定了泛型的下限。</code></p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//该方法允许传入一个泛型列表类，且其泛型可以是任意</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token class-name">PrintGeneric</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token operator">&lt;</span><span class="token operator">?</span>  l<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token comment">//该方法允许传入一个泛型列表类，但其类型必须是A类及其子类</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token class-name">PrintGeneric</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token operator">&lt;</span><span class="token operator">?</span> <span class="token keyword">extends</span> <span class="token class-name">A</span>  l<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token comment">//该方法允许传入一个泛型列表类，但其类型必须是A类及其父类</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token class-name">PrintGeneric</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token operator">&lt;</span><span class="token operator">?</span> <span class="token keyword">super</span> <span class="token class-name">A</span>  l<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="44-JUnit"><a href="#44-JUnit" class="headerlink" title="44. JUnit"></a>44. JUnit</h3><blockquote>
<p>一个类有功能代码需要测试，就需要写入到一个main方法中。  如果有多个功能代码需要测试就需要来回注释，解开注释，切换很麻烦。如果可以直接运行一个方法们就会方便很多，并且可以给出相关信息。</p>
</blockquote>
<h3 id="45-java绘图"><a href="#45-java绘图" class="headerlink" title="45. java绘图"></a>45. java绘图</h3><h4 id="java绘图体系"><a href="#java绘图体系" class="headerlink" title="java绘图体系"></a>java绘图体系</h4><blockquote>
<p> java中坐标系，坐标远点位于左上角，以像素为单位。在java坐标系中，第一个是x坐标，表示当前位置为水平方向。距离坐标原点x个像素，第二个是y坐标，表示当前位置为垂直方向，距离坐标原点y个像素。</p>
</blockquote>
<p>  符合计算机图形学知识体系。</p>
<p>  像素是一个密度单位，并不是长度单位。</p>
<p><a target="_blank" rel="noopener" href="https://imgtu.com/i/b4G1SJ"><img src="D:/文档/文档22.7.26/img/b4G1SJ.png" alt="b4G1SJ.png"></a></p>
<p>  java中画图过程：</p>
<ol>
<li>定义一个画板类，继承Jpanel类。</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Mypanel</span> <span class="token keyword">extends</span> <span class="token class-name">JPanel</span><span class="token punctuation">&#123;</span>
 <span class="token annotation punctuation">@Override</span>
 <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">paint</span><span class="token punctuation">(</span><span class="token class-name">Graphics</span> g<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
     <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">paint</span><span class="token punctuation">(</span>g<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//调用父类方法完成初始化。</span>
 <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ol>
<li>重写父类的paint方法。在paint方法中调用绘制图像的方法。</li>
<li>将主方法所在类继承窗口框架类 JFram。</li>
<li>在主方法创建画板对象。</li>
<li>实例化继承窗口框架类对象，将画板添加至窗口框架类对象。</li>
<li>设置窗口参数。</li>
</ol>
<h4 id="Component类"><a href="#Component类" class="headerlink" title="Component类"></a>Component类</h4><p>  Component类提供了两个和绘图相关的最重要的方法；</p>
<ol>
<li>paint(Graphics g)绘制组件的外观</li>
<li>repaint()刷新组件的外观。</li>
</ol>
<h4 id="paint方法"><a href="#paint方法" class="headerlink" title="paint方法"></a>paint方法</h4><p>  paint(Graphics g)绘制组件的外观。</p>
<p>  以下情况paint()方法会再次被调用。</p>
<ol>
<li>当组件第一次在屏幕显示的时候，程序会自动的调用paint()方法来绘制组件。</li>
<li>窗口最小化，再最大化。</li>
<li>窗口的大小发生变化。</li>
<li>repaint方法被调用。</li>
</ol>
<p>例子：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> draw <span class="token keyword">extends</span> <span class="token class-name">JFrame</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span>  <span class="token class-name">Mypanel</span> mp <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">new</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">public</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//初始化面板</span>
        mp <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Mypanel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//将面板放入窗口框架中</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>mp<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//设置窗口大小</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setSize</span><span class="token punctuation">(</span><span class="token number">400</span><span class="token punctuation">,</span><span class="token number">300</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token comment">//当点击关闭窗口时退出程序</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setDefaultCloseOperation</span><span class="token punctuation">(</span><span class="token class-name">JFrame</span><span class="token punctuation">.</span><span class="token constant">EXIT_ON_CLOSE</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setVisible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//可以显示</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>


<span class="token keyword">class</span> <span class="token class-name">Mypanel</span> <span class="token keyword">extends</span> <span class="token class-name">JPanel</span><span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">paint</span><span class="token punctuation">(</span><span class="token class-name">Graphics</span> g<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">paint</span><span class="token punctuation">(</span>g<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//调用父类方法完成初始化。</span>
        <span class="token comment">//调用方法绘制图像</span>
        g<span class="token punctuation">.</span><span class="token function">drawOval</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="Graphics类"><a href="#Graphics类" class="headerlink" title="Graphics类"></a>Graphics类</h4><p>  Graphics类中提供了各种各样绘制图形的方法。</p>
<ol>
<li><p>画直线：drawLine(int x1,int y1,int x2,int y2);</p>
</li>
<li><p>画矩形边框: drawRect(int x1,int y1,int width,int height);</p>
</li>
<li><p>画椭圆边框: drawOval(int x1,int y1,int width,int height);</p>
</li>
<li><p>填充矩形: fillRect(int x,int y,int width,int height); </p>
</li>
<li><p>填充矩椭圆: fillOval(int x,int y,int width,int height); </p>
</li>
<li><p>画图片：drawimage(imge img,int x,int y,…)；</p>
</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Image</span> img <span class="token operator">=</span> <span class="token class-name">Toolkit</span><span class="token punctuation">.</span><span class="token function">getDefaultToolkit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getImage</span><span class="token punctuation">(</span><span class="token class-name">Panel</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">.</span><span class="token function">getResource</span><span class="token punctuation">(</span><span class="token string">"/doubao.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
g<span class="token punctuation">.</span><span class="token function">drawImage</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">400</span><span class="token punctuation">,</span><span class="token number">500</span><span class="token punctuation">,</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<ol start="7">
<li><p>画字符串: drawString(String str,int x,int y); //这里设置的位置是字符串的左下角。</p>
</li>
<li><p>设置画笔的字体 setFont(Font font);</p>
</li>
</ol>
<p>  <code>g.setFont(new Font(&quot;字体&quot;,颜色,大小));</code></p>
<ol start="9">
<li>设置画笔的颜色: setColor(Color c);</li>
</ol>
<h3 id="46-java事件处理机制"><a href="#46-java事件处理机制" class="headerlink" title="46. java事件处理机制"></a>46. java事件处理机制</h3><p>  java种事件处理采取的是”委派事件模型”。当事件发生时，产生事件的对象，会把此类 “信息” 传递给 “事件的监听者处理” ，这里的 “信息” 实际上就是java.aet.event 事件类库里某个类所创建的对象，把它称为事件的对象。</p>
<p><a target="_blank" rel="noopener" href="https://imgtu.com/i/bIOnED"><img src="D:/文档/文档22.7.26/img/bIOnED.md.png" alt="bIOnED.md.png"></a></p>
<p>  事件处理机制分为：事件源，事件，事件监听器。</p>
<ul>
<li>事件源：事件源是一个产生事件的对象，比如按钮，窗口。</li>
<li>事件：事件就是承载事件源状态改变的对象，比如当键盘事件，鼠标事件，窗口事件等等。会生成一个事件对象，该对象保存这当前事件很多信息，比如KeyEvent对象有被按下键的Code值。java.awt.event包和javax.swing.event包种定义了各种事件类型。</li>
<li>事件监听器接口：<ol>
<li>当事件源产生一个事件，可以传送给事件监听者处理。</li>
<li>事件监听者实际上就是一个类，给类实现了某个事件监听器接口。</li>
<li>事件监听器接口有多种，不同的事件监听器接口可以监听不同的事件，一个类可以实现多个接口。</li>
<li>这些接口在java.awt.event包和javax.swing.event包中定义。</li>
</ol>
</li>
</ul>
<h3 id="47-多线程基础"><a href="#47-多线程基础" class="headerlink" title="47.  多线程基础"></a>47.  多线程基础</h3><p>  线程相关概念：</p>
<ol>
<li>程序(program)：是为了完成特定的任务，用某种语言编写的一组指令的集合。</li>
<li>进程：进程就是运行中的程序，启动一个进程，操作系统就会为该进程分配内存空间。进程是程序的一次执行过程，或者是正在运行的一个程序，是动态的过程：它有自身的产生，存在和消亡。</li>
<li>线程：线程是由进程创建的，是进程的一个实体。一个进程可以拥有多个线程。<ol>
<li>单线程：同一时刻，只允许执行一个线程。</li>
<li>多线程：同一时刻是可执行多个线程。</li>
<li>并发：同一时刻，多个任务交替执行，造成一种”貌似同时“的错觉，单核cpu实现的多任务就是并发。</li>
<li>并行：同一时刻，多个任务同时执行，多核cpu可以实现并行。并发和并行可以同时存在。</li>
</ol>
</li>
</ol>
<h4 id="47-1-创建线程"><a href="#47-1-创建线程" class="headerlink" title="47.1 创建线程"></a>47.1 创建线程</h4><ol>
<li>继承Thread类，重写run方法。</li>
<li>实现Runnable接口，重写run方法。</li>
</ol>
<p>  run方法是实现了Runnable接口的run方法。</p>
<p><a target="_blank" rel="noopener" href="https://imgtu.com/i/bosvh4"><img src="https://s1.ax1x.com/2022/03/11/bosvh4.png" alt="bosvh4.png"></a></p>
<h5 id="继承thread类"><a href="#继承thread类" class="headerlink" title="继承thread类"></a>继承thread类</h5><p>  当主线程执行子线程时主线程不会阻塞</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Thread01</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">Cat</span> cat <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Cat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        cat<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
 
 
<span class="token comment">//1.当一个类继承了Thread类，该类就可以当作线程使用</span>
<span class="token keyword">class</span> <span class="token class-name">Cat</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span><span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"线程正在执行"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
            <span class="token comment">//线程休眠</span>
            <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
                <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="实现Runnable接口"><a href="#实现Runnable接口" class="headerlink" title="实现Runnable接口"></a>实现Runnable接口</h5><ol>
<li>创建类实现Runnable接口,实现run方法.</li>
<li>创建对应类的对象.</li>
<li>创建Thread类的对象,将实现Runnable接口类的对象传入Thread的构造方法.</li>
<li>使用Thread类对象调用start()方法创建新的线程.</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Thread01</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">Cat2</span> ca <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Cat2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Thread</span> thread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>ca<span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">class</span> <span class="token class-name">Cat2</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"次要线程"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
 
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>  将实现Runnable的类的对象传入Thread类的构造方法,是使用了静态代理模式.</p>
<h4 id="47-2-调用线程"><a href="#47-2-调用线程" class="headerlink" title="47.2 调用线程"></a>47.2 调用线程</h4><ul>
<li>创建线程后实例化对象,再调用对应对象的start方法进行使用线程.</li>
</ul>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ThreadClass</span> tc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        tc<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<ul>
<li>将实例化的对象传入Thread构造方法中,调用Tread类对象的start方法进行使用进程.</li>
</ul>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Cat2</span> ca <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Cat2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Thread</span> thread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>ca<span class="token punctuation">)</span><span class="token punctuation">;</span>
thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>  start()方法调用开启一个新的线程,和主线程轮转运行.</p>
<p>  run()方法调用,run()方法只是一个普通的方法,不开启新的线程,还是主线程调用run()中的代码.</p>
<p>  start()方法调用底层start0()方法后.==该线程并不会立即执行,只是将该线程变成可运行状态,具体什么事件执行,取决于cpu由cpu统一调度.==</p>
<ul>
<li>start0()是本地方法,是JVM调用,底层是C/C++实现,真正实现多线程的效果是start0(),而不是run();</li>
<li>一个线程对象只能调用一次start()方法启动，如果重复调用了，则将抛出以上的异常“IllegalThreadStateException” 。</li>
</ul>
<h4 id="47-3-线程终止"><a href="#47-3-线程终止" class="headerlink" title="47.3 线程终止"></a>47.3 线程终止</h4><p>  当线程完成任务时自动退出.</p>
<p>  可以通过使用变量来控制run方法退出的方式停止线程,即通知方式.</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Thread03</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">Threade</span> thread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Threade</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        thread<span class="token punctuation">.</span><span class="token function">setLoop</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">class</span> <span class="token class-name">Thsda</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span><span class="token punctuation">&#123;</span>
  
  <span class="token comment">//定义私有变量控制线程的for循环</span>
    <span class="token keyword">private</span> <span class="token keyword">boolean</span> loop <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>loop<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"线程运行中"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
	<span class="token comment">//私有属性的set方法,可以控制通知变量.</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setLoop</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> loop<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>loop <span class="token operator">=</span> loop<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="47-4-线程常用方法"><a href="#47-4-线程常用方法" class="headerlink" title="47.4 线程常用方法"></a>47.4 线程常用方法</h4><p>  SetName()  //设置线程名称</p>
<p>  getName() //获得该线程的名称</p>
<p>  start()   //使该线程开始执行</p>
<p>  run()  //调用线程对象run方法</p>
<p>  setPriority()  //更改线程的优先级</p>
<p>  getPriority()   //获取线程的优先级</p>
<p>  sleep() //在指定的毫秒数内让正在执行的线程休眠.==静态方法==</p>
<p>  interrupt()  //中断线程,并不是结束线程,抛出InterruptedExcepotion异常.</p>
<p>  yield:线程的礼让.让出cpu,让其他线程执行,但礼让的事件不确定,所以也不一定礼让的事件不确定,所以也不一定礼让成功.</p>
<p>  join: 线程的插队,插队的线程一旦插队成功,则肯定限制运行插入线程的所有的任务.(再被插队的线程中调用插队线程的join方法)</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//主线程中</span>
<span class="token class-name">Thead1</span> th <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thead1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
th<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//主线程和th分线程交替执行</span>
th<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">//将th分线程插入到主线程前，先执行完分线程再执行主线程。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p> 用户线程和守护线程</p>
<ol>
<li>用户线程：也叫工作线程，当线程的任务执行完成，或者通知方式结束</li>
<li>守护线程：一般是为工作线程服务的，==当所有的用户线程结束，守护线程自动结束==。  (垃圾回收机制)</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//主线程中</span>
<span class="token class-name">Thead1</span> th <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thead1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//设置指定线程为守护线程，在启动相应的线程</span>
th<span class="token punctuation">.</span><span class="token function">setDeamon</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
th<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="47-5-线程的生命周期"><a href="#47-5-线程的生命周期" class="headerlink" title="47.5. 线程的生命周期"></a>47.5. 线程的生命周期</h4><blockquote>
<p>JDK中使用Thread.State枚举表示了线程的几种状态。</p>
</blockquote>
<ul>
<li>New: 尚未启动的线程处于此状态。</li>
<li>Runnable: 启动或者正在Java虚拟机中执行的线程处于此状态</li>
<li>Teminated:  线程终止。</li>
<li>Time_Waiting: 正在等待另一个线程执行动作达到指定等待时间的线程处于此状态。</li>
<li>Warting: 正在等待另一个线程执行特定动作的线程处于此状态。</li>
<li>Blocked: 被阻塞等待监视器锁定的线程处于此状态。</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://imgtu.com/i/bqPoM6"><img src="https://s1.ax1x.com/2022/03/13/bqPoM6.png" alt="bqPoM6.png"></a></p>
<h4 id="47-6-线程同步机制"><a href="#47-6-线程同步机制" class="headerlink" title="47.6. 线程同步机制"></a>47.6. 线程同步机制</h4><blockquote>
<p> 在多线程编程中，一些敏感数据不允许被多个线程同时访问，此时就使用同步访问技术，保证数据在任何任何时刻，最多有一个线程访问，以保证数据的完整性。</p>
</blockquote>
<p>  线程同步：即当有一个线程对内存进行操作时，其他线程都不可以对这个内存地址进行操作，直到该线程完成操作，其他线程才能对该内存地址进行操作。</p>
<h5 id="同步方法：Synchronized"><a href="#同步方法：Synchronized" class="headerlink" title="同步方法：Synchronized"></a>同步方法：Synchronized</h5><ol>
<li><p>同步代码块：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">synchronized</span> <span class="token punctuation">(</span>对象<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token comment">//得到对象的锁，才能操作同步代码</span>
            <span class="token comment">//需要被同步的代码</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></li>
<li><p>Synchronized 还可以放到方法声明中，表示整个方法为同步方法：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">synchronized</span> <span class="token keyword">void</span> <span class="token function">m</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  
            <span class="token comment">//需要被同步的代码</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></li>
</ol>
<h5 id="互斥锁"><a href="#互斥锁" class="headerlink" title="互斥锁"></a>互斥锁</h5><ul>
<li>java语言中，引入了对象互斥锁的概念，来保证共享数据操作的完整性。</li>
<li>每个对象都对应于一个可称为“互斥锁”的标记，这个标记用来保证在任一时刻，只有一个线程访问该对象。                </li>
<li>关键字synchronized来与对象的互斥锁联系。当某个对象用synchronized修饰时，==表明该对象在任一时刻只能由一个线程访问==。</li>
<li>同步的局限性：导致程序的执行效率要降低。</li>
<li>同步方法(非静态的) 的锁可以是this，也可以是其他对象(==要求是同一个对象==)</li>
<li>同步方法(静态的) 的锁为当前类本身，不是this。</li>
<li>同步方法的锁：静态方法（类名.class） 、 非静态方法（this）</li>
<li>同步代码块：自己指定， 很多时候也是指定为this或类名.class</li>
</ul>
 <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> 	start <span class="token keyword">void</span> <span class="token function">m</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  
	<span class="token keyword">synchronized</span> <span class="token punctuation">(</span>当前类名<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>注意：<ul>
<li>必须确保使用同一个资源的多个线程共用一把锁， 这个非常重要， 否则就无法保证共享资源的安全</li>
<li>==一个线程类中的所有静态方法共用同一把锁（类名.class），所有非静态方法共用同一把锁（this），同步代码块（指定需谨慎）== </li>
</ul>
</li>
</ul>
<p>​        </p>
<h4 id="47-7-线程的死锁"><a href="#47-7-线程的死锁" class="headerlink" title="47.7. 线程的死锁"></a>47.7. 线程的死锁</h4><blockquote>
<p> 多个线程都占用了对方的锁资源，但是都不肯相让，导致了死锁，在编程是一定要避免死锁的发生。</p>
</blockquote>
<h4 id="47-8-释放锁"><a href="#47-8-释放锁" class="headerlink" title="47.8 释放锁"></a>47.8 释放锁</h4><p>  下面的操作会释放锁：</p>
<ul>
<li>当前线程的同步方法，同步代码块结束。</li>
<li>当前线程在同步代码块，同步方法中遇到break，return。</li>
<li>当前线程在同步代码块，同步方法中出现了未处理的Error和Excepotion，导致异常结束，</li>
<li>当前线程在同步代码块，同步方法中执行了线程对象的wait()方法，当前线程暂停，并释放锁。</li>
</ul>
<p>  下面的操作不会释放锁：</p>
<ul>
<li>线程执行同步代码块或者同步方法时，程序调用Thread.sleep(),Thread.yield()方法暂停当前线程的执行，不会释放锁。</li>
<li>线程执行同步代码块时，其他线程调用了该线程的suspend()方法将该线程挂起。</li>
</ul>
<h3 id="48-IO流"><a href="#48-IO流" class="headerlink" title="48. IO流"></a>48. IO流</h3><p>  文件：文件就是保存数据的地方。 </p>
<p>  文件流： 文件在程序中是以流的形式来操作的。</p>
<ul>
<li>流：数据在数据源(文件)和程序(内存)之间经历的路径。</li>
<li>输入流：数据从数据源(文件)到程序(内存)的路径。</li>
<li>输出流：数据从程序(内存)到数据源(文件)的路径。</li>
<li>File实现了Serializable和Comparable接口可以串行化和排序。</li>
</ul>
<p><img src="./img/io%E6%8E%A5%E5%8F%A3%E5%9B%BE.png"></p>
<h4 id="File创建文件对象相关构造器"><a href="#File创建文件对象相关构造器" class="headerlink" title="File创建文件对象相关构造器"></a>File创建文件对象相关构造器</h4><p>  File中的路径中不同系统中路径分隔符不同。</p>
<p>  构造器：</p>
<ul>
<li><p>new File(String pathname)  //根据路径构建一个File对象。</p>
</li>
<li><p>使用createNewFile()方法可以创建对应文件。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"D:\\writ\\news1.txt"</span><span class="token punctuation">;</span>
<span class="token class-name">File</span> file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
file<span class="token punctuation">.</span><span class="token function">createNewFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"文件创建成功"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
<ul>
<li><p>new File(File parent,String child)  //根据父目录文件 + 子路径构建</p>
<pre class="line-numbers language-JAVA" data-language="JAVA"><code class="language-JAVA">String parentFilestr &#x3D; &quot;D:\\writ\\&quot;;
String fileName &#x3D; &quot;new2.txt&quot;;

	 File parentFile &#x3D; new File(parentFilestr,fileName1);
File file &#x3D; new File(parentFile,fileName2);
try &#123;
file.createNewFile();
&#125; catch (IOException e) &#123;
e.printStackTrace();
&#125;
System.out.println(&quot;文件创建成功&quot;);<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
<ul>
<li><p>new File(String parent,String child) //根据父目录路径 + 子路径构建</p>
<pre class="line-numbers language-JAVA" data-language="JAVA"><code class="language-JAVA">String parentFile &#x3D; &quot;D:\\writ\\&quot;;
String filePath &#x3D; &quot;new3.txt&quot;;
File file &#x3D; new File(parentFile,filePath);
try &#123;
file.createNewFile();
&#125; catch (IOException e) &#123;
e.printStackTrace();
&#125;
System.out.println(&quot;文件创建成功&quot;);<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
<h4 id="常用文件的操作"><a href="#常用文件的操作" class="headerlink" title="常用文件的操作"></a>常用文件的操作</h4><p>  getName:  获取文件的名字</p>
<p>  getAbsolutePath:  获取文件的据对路径</p>
<p>  getParent:  获取文件的父级目录</p>
<p>  Iength: 获取文件的大小(字节)</p>
<p>  exists:   是否存在文件</p>
<p>  isFile:  是否是一个文件</p>
<p>  isDiectory:  是否是一个路径；</p>
<p>  mkdir:创建一级目录。</p>
<ul>
<li>创建单级目录，返回一个boolean值</li>
</ul>
  <pre class="line-numbers language-JAVA" data-language="JAVA"><code class="language-JAVA">String parentFile &#x3D; &quot;D:\\writ\\newFile&quot;;
File file &#x3D; new File(parentFile);
if (file.mkdir()) &#123;
System.out.println(&quot;成功&quot;);
&#125;else&#123;
System.out.println(&quot;失败&quot;);
&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>  mkdirs:创建多级目录</p>
<ul>
<li>返回多级目录，返回一个boolean值</li>
</ul>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> parentFile <span class="token operator">=</span> <span class="token string">"D:\\writ\\newFile\\jbz\\cql"</span><span class="token punctuation">;</span>
<span class="token class-name">File</span> file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>parentFile<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">mkdirs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"成功"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"失败"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>delete:==删除空目录==或者文件</li>
</ul>
<p>  在java编程中目录被当作特殊的文件。</p>
<h4 id="IO流原理及流的分类"><a href="#IO流原理及流的分类" class="headerlink" title="IO流原理及流的分类"></a>IO流原理及流的分类</h4><ul>
<li><p>Java IO流原理：</p>
<ul>
<li>IO是Input/Output的缩写，IO技术是非常实用的技术，用于处理数据传输。如读写文件，网络通讯。</li>
<li>Jave程序中，对于数据的输入输出操作以流(stream)的方式进行。</li>
<li>java.io包下提供了各种流类和接口，用以获取不同种类的数据，并通过方法输入或者输出数据。</li>
<li>输入input：读取外部数据(磁盘，光盘等存储设备的数据)到程序(内存)中。</li>
<li>输出output: 将程序(内存)数据输出到磁盘，光盘等存储设备中。</li>
</ul>
</li>
<li><p>流的分类：</p>
<ul>
<li>按照操作数据的单位不同分为： 字节流(8bit，对二进制文件如声音文件比较好),字符流(按字符对文本文件操作比较好)</li>
<li>按照数据流的流向不同分为：  输入流，输出流</li>
<li>按照流的角色的不同分为：节点流，处理流/包装流</li>
</ul>
<table>
<thead>
<tr>
<th>抽象基类</th>
<th>字节流</th>
<th>字符流</th>
</tr>
</thead>
<tbody><tr>
<td>输入流</td>
<td>InputStream</td>
<td>Reader</td>
</tr>
<tr>
<td>输出流</td>
<td>OutputStream</td>
<td>Writer</td>
</tr>
</tbody></table>
<ol>
<li>java的io流共涉及40多个类，实际上非常规则，都是从如上四个抽象基类中派生的。</li>
<li>由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。</li>
</ol>
</li>
</ul>
<h4 id="InputStream-字节输入流"><a href="#InputStream-字节输入流" class="headerlink" title="InputStream 字节输入流"></a>InputStream 字节输入流</h4><p>  inputStream抽象类是所有类字节输入流的超类。</p>
<p>  inputStream常用子类：</p>
<ul>
<li>FileInputStream： 文件输入流</li>
<li>BufferedInputStream ：处理字节输入流</li>
<li>ObjectInputStream： 对象字节输入流</li>
</ul>
<h4 id="OutputStream-字节输出流"><a href="#OutputStream-字节输出流" class="headerlink" title="OutputStream 字节输出流"></a>OutputStream 字节输出流</h4><p>  OutputStream 抽象类是所有类字节输出流的超类。</p>
<p>  OutputStream 常用子类：</p>
<ul>
<li>FileOutputStream ： 文件输出流</li>
<li>BufferedOutputStream  ： 处理字节输出流</li>
<li>ObjectOutputStream ： 对象字节输出流</li>
</ul>
<h4 id="FileInputStream-文件字节输入流"><a href="#FileInputStream-文件字节输入流" class="headerlink" title="FileInputStream  文件字节输入流"></a>FileInputStream  文件字节输入流</h4><p>  构造器：</p>
<p>  ​    FileInputStream  f = new FileInputStream(文件路径);</p>
<p>  方法：</p>
<ul>
<li><p>read();</p>
<ul>
<li>对指定文件读入单个字节。</li>
<li>如果读取完毕返回-1,正常读取返回char的码值。</li>
</ul>
</li>
<li><p>read(byte[] b)</p>
<ul>
<li><p>传入一个字符数组，对指定文件读入指定字符数组的长度存储在传入的字符数组中。</p>
</li>
<li><p>==如果读取完毕返回-1，未到结尾返回读取的个数==；</p>
</li>
<li><p>这个返回的数组只是存储读取到的字符，如果到达尽头没有读取到数组长度的字符，则之前读取过的字符不会刷新还是维持之前保存的字符。</p>
</li>
<li><p>可以结合new String(字节数组，开始，结束)构造器得到字符。</p>
</li>
</ul>
</li>
</ul>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//----------------------------read()---------------</span>
<span class="token keyword">int</span> readData<span class="token punctuation">;</span>
<span class="token class-name">String</span> parentFile <span class="token operator">=</span> <span class="token string">"D:\\writ\\jbz.txt"</span><span class="token punctuation">;</span>
<span class="token class-name">FileInputStream</span> fileInputStream <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
     fileInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>parentFile<span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>readData <span class="token operator">=</span> fileInputStream<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
           <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span>readData<span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
       e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">finally</span> <span class="token punctuation">&#123;</span>
     <span class="token comment">//关闭输入流</span>
    fileInputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">//----------------------------read(byte[] buf)---------------</span>
 
<span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> readLen<span class="token punctuation">;</span>
<span class="token class-name">String</span> parentFile <span class="token operator">=</span> <span class="token string">"D:\\writ\\jbz.txt"</span><span class="token punctuation">;</span>
<span class="token class-name">FileInputStream</span> fileInputStream <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
     fileInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>parentFile<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">//正常读取返回读取到的字节数，已经到达结束未读取到字节返回-1</span>
  <span class="token comment">//如果读取到的长度不到指定字节数组的长度则再下一次读取返回-1，此次读取只返回读取的字节长度。</span>
	<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>readLen <span class="token operator">=</span> fileInputStream<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
      		<span class="token comment">//利用返回的长度使用String构造器读取字符串。</span>
           <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>readLen<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
       e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">finally</span> <span class="token punctuation">&#123;</span>
      <span class="token comment">//关闭输入流</span>
	fileInputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="FileOutputStream-文件字节输出流"><a href="#FileOutputStream-文件字节输出流" class="headerlink" title="FileOutputStream  文件字节输出流"></a>FileOutputStream  文件字节输出流</h4><p>  构造器：</p>
<p>  ​    FileOutputStream  file = new FileOutputStream (文件路径);</p>
<p>  ​    FileOutputStream  file = new FileOutputStream (文件路径，是否以添加形式);   //当是否以添加形式为true时，写入的字节将添加到文件的末尾，而不是替换。</p>
<p>  方法：</p>
<p>  ​    write( byte );  将指定字节写入指定文件中 </p>
<p>  ​    write( str.getBytes() );  </p>
<p>  ​    write( str.getBytes() ,开始，结尾); </p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">FileOutputStream</span> fil <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> parentFile <span class="token operator">=</span> <span class="token string">"D:\\writ\\jbz.txt"</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//设置将字符添加到文件末尾</span>
            fil <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>parentFile<span class="token punctuation">,</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"ddddd"</span><span class="token punctuation">;</span>
            fil<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            fil<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token char">'s'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="FileReader-文件字符输入流"><a href="#FileReader-文件字符输入流" class="headerlink" title="FileReader  文件字符输入流"></a>FileReader  文件字符输入流</h4><p>  FileReader 和 FileWrite是字符流，即按照字符来操作io。</p>
<p>  构造器：</p>
<p>  ​    FileReader(文件路径)；</p>
<p>  方法：</p>
<p>  ​    read()每次读取单个字符，返回该字符，如果到文件末尾返回-1；</p>
<p>  ​    read(char[]): 批量读取多个字符到数组，返回读取到的字符数，如果到文件末尾返回-1.</p>
<p>  ​    ==返回的char数组重复使用的时候不会清空而是只覆盖获得数组长度的位置。==</p>
<p>  相关API：</p>
<p>  ​    new String(char[]):将char[]转换为String；</p>
<p>  ​    new String(char[],off,len):将char[]的指定部分转换成String。</p>
<h4 id="FileWrite-文件字符输出流"><a href="#FileWrite-文件字符输出流" class="headerlink" title="FileWrite  文件字符输出流"></a>FileWrite  文件字符输出流</h4><p>  FileReader 和 FileWrite是字符流，即按照字符来操作io。</p>
<p>  构造器：</p>
<p>  ​    FileWrite(文件路径);    //覆盖模式，相当于流的指针在首端。</p>
<p>  ​    FileWrite(文件路径，true); //追加模式，相当于流的指针在尾端。</p>
<p>  方法：</p>
<p>  ​    write(int); 写入单个字符。</p>
<p>  ​    write(char[]); 写入指定数组。</p>
<p>  ​    write(char[],off,len) 写入指定数组的指定部分。</p>
<p>  ​    write(String)；  写入整个字符串</p>
<p>  ​     write(String,off,len) 写入指定字符串的指定部分。</p>
<p>  相关API：</p>
<p>  ​    toCharArray 将指定String转换成char[];</p>
<p>  注意：==FileWriter使用后，必须要关闭(close)或者刷新(flush)，否则写入不到指定的文件。==</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">FileReader</span> fr <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> le <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> filp <span class="token operator">=</span> <span class="token string">"D:\\writ\\jbz.txt"</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
      fr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span>filp<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>le <span class="token operator">=</span> fr<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>le<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
       e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
       e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">finally</span> <span class="token punctuation">&#123;</span>
<span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
          fr<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
         e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="节点流和处理流区别"><a href="#节点流和处理流区别" class="headerlink" title="节点流和处理流区别"></a>节点流和处理流区别</h4><p>  节点流可以从一个特定的数据源读写数据。(FileRead,FileWriter)</p>
<p>  处理流(包装流)是链接已经存在的流(处理流或节点流)之上，为程序提供更为强大的读写功能。(BufferedReader,BufferedWriter，BufferedOutputStream，BufferedInputStream)</p>
<p>节点流和处理流的区别和联系：</p>
<ol>
<li>节点流是底层流，低级流，可以直接和数据源相接。</li>
<li>处理流包装节点流，即可以消除不同节点流的实现差异，也可以提供更方便的方法来完成输入输出。</li>
<li>处理流(包装流)，对节点流进行包装，使用了修饰器设计模式，不会直接与数据源相连。</li>
</ol>
<p>处理 流功能：</p>
<ul>
<li>性能的提高：==主要以增加缓冲的方式来提高输入输出的效率。==</li>
<li>操作的便捷：处理流可以提供一系列便捷的方法来一次输入输出大批量的数据，使用更加灵活方便。</li>
</ul>
<h4 id="BufferedReader-字符输入处理流"><a href="#BufferedReader-字符输入处理流" class="headerlink" title="BufferedReader 字符输入处理流"></a>BufferedReader 字符输入处理流</h4><p>  BufferedReader 和 BufferedWrite 属于字符流，是按照字符来读取数据。==关闭时，只需要关闭外层流既可==。</p>
<p>  不要使用字符流来读取二进制文件。</p>
<p>  构造器：</p>
<p>  ​    BufferedReader(reader流)</p>
<p>  方法：</p>
<p>  ​    readLine()； 按行返回字符串，如果已经到达流的结尾返回null</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> line <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"D:\\writ\\Bufferedtest.txt"</span><span class="token punctuation">;</span>
<span class="token class-name">BufferedReader</span> bufferedReader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>line <span class="token operator">=</span> bufferedReader<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
         <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
bufferedReader<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>==只需要关闭处理流(外层)，就可以在底层代码中关闭节点流。==</li>
</ul>
<h4 id="BufferedWriter-字符输出处理流"><a href="#BufferedWriter-字符输出处理流" class="headerlink" title="BufferedWriter 字符输出处理流"></a>BufferedWriter 字符输出处理流</h4><p>  BufferedReader 和 BufferedWrite 属于字符流，是按照字符来读取数据。==关闭时，只需要关闭外层流既可==。</p>
<p>  不要使用字符流来读取二进制文件。</p>
<p>  构造器：</p>
<p>  ​    BufferedWriter(Writer流);</p>
<p>  ​    BufferedWriter( Writer流(文件路径,true) );</p>
<p>  方法：</p>
<p>  ​    writer( String );  //插入指定字符串,不会换行.</p>
<p>  ​    newLine();  //插入一个换行，与系统相关的换行</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> line <span class="token operator">=</span> <span class="token string">"开始的开始我们都是孩子"</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"D:\\writ\\Bufferedtest.txt"</span><span class="token punctuation">;</span>
<span class="token class-name">BufferedWriter</span> bufferedWriter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedWriter</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
bufferedWriter<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>
bufferedWriter<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="BufferedInputStream-字节输入处理流"><a href="#BufferedInputStream-字节输入处理流" class="headerlink" title="BufferedInputStream 字节输入处理流"></a>BufferedInputStream 字节输入处理流</h4><p>  BufferedInputStream是字节流，在创建BufferedInputStream时，会创建一个内部缓冲区数组。</p>
<p>  构造器：</p>
<p>  ​    BufferedInputStream( InputStream流 )；</p>
<p>  ​    BufferedInputStream( InputStream流 ，size)；</p>
<h4 id="BufferedOutputStream-字节输出处理流"><a href="#BufferedOutputStream-字节输出处理流" class="headerlink" title="BufferedOutputStream 字节输出处理流"></a>BufferedOutputStream 字节输出处理流</h4><p>  BufferedOutputStream是字节流，在创建BufferedOutputStream 时，会创建一个内部缓冲区数组。</p>
<p>  构造器：</p>
<p>  ​    BufferedOutputStream(OutputStream流);</p>
<p>  ​    BufferedOutputStream(OutputStream流(路径,true));</p>
<h4 id="ObjectOutputStream"><a href="#ObjectOutputStream" class="headerlink" title="ObjectOutputStream"></a>ObjectOutputStream</h4><p>  能够将基本数据类型或者对象进行序列化。使用修饰者模式。</p>
<ul>
<li><p>序列化：保存数据时，保存数据的值和数据类型。</p>
</li>
<li><p>反序列化： 将保存的数据回复值和数据类型。</p>
</li>
<li><p>序列化后，保存的文件格式，不以保存文件后缀保存，以自身的格式来保存。</p>
</li>
<li><p>需要让某个对象支持序列化机制，则必须让其类时可序列化的，为了让某个类时可序列化的，==该类必须实现如下两个接口之一==：</p>
<ul>
<li>Serializable   //标记接口，没有任何方法</li>
<li>Externalizable  //该接口有方法需要实现</li>
</ul>
</li>
<li><p>方法：</p>
<ul>
<li>writeInt(100);          //保存int类型</li>
<li>writeBoolean(true);  //保存boolean类型</li>
<li>writeDouble(1.2);   //保存double</li>
<li>writeUTF(“你好”);  //保存字符串</li>
<li>writeChar(‘s’);  //保存char</li>
<li>oos.writeObject(对象);  //保存实现接口的对象</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"D:\\writ\\wht.txt"</span><span class="token punctuation">;</span>
        <span class="token class-name">ObjectOutputStream</span> oos <span class="token operator">=</span> <span class="token keyword">new</span> 			 						<span class="token class-name">ObjectOutputStream</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        oos<span class="token punctuation">.</span><span class="token function">writeInt</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//自动进行装箱操作</span>
        oos<span class="token punctuation">.</span><span class="token function">writeBoolean</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//自动进行装箱操作</span>
        oos<span class="token punctuation">.</span><span class="token function">writeDouble</span><span class="token punctuation">(</span><span class="token number">1.2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//自动进行装箱操作</span>
        oos<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
<h4 id="ObjectInputStream"><a href="#ObjectInputStream" class="headerlink" title="ObjectInputStream"></a>ObjectInputStream</h4><p>  能够将基本数据类型或者对象进行反序列化。使用修饰者模式。</p>
<ul>
<li>序列化：保存数据时，保存数据的值和数据类型。</li>
<li>反序列化： 将保存的数据回复值和数据类型。</li>
<li>需要让某个对象支持序列化机制，则必须让其类时可序列化的，为了让某个类时可序列化的，该类必须实现如下两个接口之一：<ul>
<li>Serializable   //标记接口，没有任何方法</li>
<li>Externalizable  //该接口有方法需要实现</li>
</ul>
</li>
<li>方法：</li>
<li>readInt();  //保存int类型</li>
<li>readBoolean();  //保存boolean类型</li>
<li>readDouble();   //保存double</li>
<li>readUTF();  //保存字符串</li>
<li>readChar();  //保存char</li>
</ul>
<ul>
<li><p>==读取的顺序需要和序列化的顺序一致。==</p>
</li>
<li><p>==反序列化对象，对应的类应该被定义。==</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ObjectInputStream</span> ois <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectInputStream</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ois<span class="token punctuation">.</span><span class="token function">readInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ois<span class="token punctuation">.</span><span class="token function">readBoolean</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ois<span class="token punctuation">.</span><span class="token function">readDouble</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ois<span class="token punctuation">.</span><span class="token function">readUTF</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ois<span class="token punctuation">.</span><span class="token function">readChar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
<p>  注意细节:</p>
<ol>
<li><p>==读写顺序需要一致.==</p>
</li>
<li><p>==要求实现序列化或者反序列化对象,需要实现Serializable==</p>
</li>
<li><p>序列化的类中添加SerialVersionUID,为了提高版本的兼容性.</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">long</span> serialVersionUID <span class="token operator">=</span> <span class="token number">1L</span><span class="token punctuation">;</span>
<span class="token comment">//序列化的版本号,实现兼容性.</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></li>
<li><p>序列化对象时,默认将里面的所有属性都进行序列化但是除了static和transient修饰的成员.</p>
</li>
<li><p>序列化对象时,要求里面的属性的类型也需要实现对应的序列化接口了.</p>
</li>
<li><p>序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现序列化.</p>
</li>
</ol>
<h4 id="标准输入输出流"><a href="#标准输入输出流" class="headerlink" title="标准输入输出流"></a>标准输入输出流</h4><p>  in 编译类型: InputStream 运行类型: BufferedInputStream</p>
<p>  out 编译类型: PrintStream 运行类型: PrintStream</p>
<table>
<thead>
<tr>
<th>标准输入输出流</th>
<th>类型</th>
<th>默认设备</th>
</tr>
</thead>
<tbody><tr>
<td>System.in</td>
<td>InputSteam</td>
<td>键盘</td>
</tr>
<tr>
<td>System.out</td>
<td>PrintStream</td>
<td>显示器</td>
</tr>
</tbody></table>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Systeam</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Scanner</span> san <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>





<h4 id="转换流定义"><a href="#转换流定义" class="headerlink" title="转换流定义"></a>转换流定义</h4><p>  字符流的默认编码是UTF-8,如果文件使用的编码不是默认的UTF-8则使用字符流读取会出现乱码现象。</p>
<p>  转换流InputStreamReader 和 OutputStreamWriter可以实现字节流向字符流的转换==并且实现指定编码方式。==</p>
<p>  InputStreamReader是Reader的子类，可以将InputStream(字节流)包装称为Reader(字符流)，并且指定编码方式;</p>
<p>  OutputStreamWriter是Writer的子类，可以将 OutputStream(字节流)包装为Writer(字符流)，并且指定编码方式；</p>
<p>  当处理纯文本数据时，如果使用字符流效率更高，并且可以有效解决中文问题，所以建议将字节流转换成字符流。</p>
<p>  可以在使用时指定编码格式： utf-8,gbk,gb2312,iso8859-1</p>
<h4 id="InputStreamReader-转换输入流"><a href="#InputStreamReader-转换输入流" class="headerlink" title="InputStreamReader 转换输入流"></a>InputStreamReader 转换输入流</h4><p>  构造器：</p>
<p>  ​    InputStreamReader(inputStream流 , 编码); 传入一个inputStream.</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"D:\\writ\\wht.txt"</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>
  <span class="token comment">//创建一个字节流，使用转换流转换为字符流，再将字符流包装为处理流</span>
        <span class="token class-name">BufferedReader</span> bfr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InputStreamReader</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">"gbk"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>str <span class="token operator">=</span> bfr<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        bfr<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<h4 id="OutputStreamWriter-转换输出流"><a href="#OutputStreamWriter-转换输出流" class="headerlink" title="OutputStreamWriter  转换输出流"></a>OutputStreamWriter  转换输出流</h4><p>  构造器：</p>
<p>  ​    OutputStreamWriter(OutputStream流，编码); 传入一个OutputStream.</p>
<p>  可以用该种方式创建指定编码方式的文件。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
     <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"D:\\writ\\wht.txt"</span><span class="token punctuation">;</span>
     <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"歌谣的歌谣，藏着童话的影子"</span><span class="token punctuation">;</span>
  <span class="token comment">//创建一个字节流使用转换流转换为字符流，再将该字符流包装为处理流</span>
     <span class="token class-name">BufferedWriter</span> bw <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedWriter</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OutputStreamWriter</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>filePath<span class="token punctuation">,</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">"gbk"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     bw<span class="token punctuation">.</span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     bw<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
     bw<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"插入成功"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="打印流定义"><a href="#打印流定义" class="headerlink" title="打印流定义"></a>打印流定义</h4><p>  打印流没有输入流只有输出流。</p>
<p>  在默认情况下输出数据的位置是显示器。</p>
<p>  PrintStream</p>
<p>  PrintWriter</p>
<h4 id="PrintStream-字节打印流"><a href="#PrintStream-字节打印流" class="headerlink" title="PrintStream 字节打印流"></a>PrintStream 字节打印流</h4><p>  构造器：</p>
<p>  ​    PrintStream(文件路径);</p>
<p>  ​    PrintStream( InputStream流 )；</p>
<p>  方法：</p>
<p>  ​    System.setOut(new PrintStream(文件路径)); //设置输出的位置</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"D:\\writ\\wht.txt"</span><span class="token punctuation">;</span>
  <span class="token comment">//设置打印位置</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">setOut</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">PrintStream</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"最后的最后"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
 
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="PrintWriter-字符打印流"><a href="#PrintWriter-字符打印流" class="headerlink" title="PrintWriter 字符打印流"></a>PrintWriter 字符打印流</h4><p>  构造器：</p>
<p>  ​    PrintWriter( Writer流 )</p>
<p>  方法：</p>
<p>  ​    print() //向指定位置输出字符</p>
<p>  ​    close() //关闭打印流。</p>
<p>  ==如果不关闭打印流，不会刷新不会打印进去。==</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"D:\\writ\\wht.txt"</span><span class="token punctuation">;</span>
        <span class="token class-name">PrintWriter</span> pw <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PrintWriter</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pw<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"开始的开始"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pw<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="Properties类"><a href="#Properties类" class="headerlink" title="Properties类"></a>Properties类</h4><p>  专门用于读写配置文件的集合类。配置文件的格式”</p>
<p>  ​                <code>键=值</code></p>
<p>  ​                <code>键=值</code></p>
<p>  键值对不需要有空格，值不需要用引号引起来，默认类型是String。</p>
<p>  方法：</p>
<ul>
<li>load() : 加载配置文件的键值对到Properties对象。</li>
<li>list() : 将数据显示到指定设备。</li>
<li>getProperty(key) : 根据键获取值。</li>
<li>setProperty(key,value): 设置键值对到Properties对象。如果没有就是创建，如果有就是替换。</li>
<li>store( InputStream流，注释)：将Properties中的键值对存储到配置文件，在idea中，保存信息到配置文件，如果含有中文，会存储为unicode码。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//获取配置文件的值</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"src\\myProperties.properties"</span><span class="token punctuation">;</span>
        <span class="token class-name">Properties</span> ps <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ps<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ps<span class="token punctuation">.</span><span class="token function">list</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> user <span class="token operator">=</span> ps<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//创建配置文件</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"src\\myProperties2.properties"</span><span class="token punctuation">;</span>
        <span class="token class-name">Properties</span> pr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pr<span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">,</span><span class="token string">"cql"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pr<span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">,</span><span class="token string">"nnn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pr<span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"pwd"</span><span class="token punctuation">,</span><span class="token string">"10086"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pr<span class="token punctuation">.</span><span class="token function">store</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"配置成功"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="ByteArrayOutputStream"><a href="#ByteArrayOutputStream" class="headerlink" title="ByteArrayOutputStream"></a>ByteArrayOutputStream</h4><pre class="line-numbers language-none"><code class="language-none">ByteArrayOutputStream bos &#x3D; new ByteArrayOutputStream();

while ((readLean &#x3D; bufferedInputStream.read(by)) !&#x3D; -1)&#123;
    bos.write(by,0,readLean);
&#125;
byte[] array &#x3D; bos.toByteArray();<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<h3 id="49-网络编程"><a href="#49-网络编程" class="headerlink" title="49.网络编程"></a>49.网络编程</h3><h4 id="ip地-址："><a href="#ip地-址：" class="headerlink" title="ip地 址："></a>ip地 址：</h4><blockquote>
<p> 概念：用于唯一表示网络中的每台计算机/主机。</p>
</blockquote>
<p>  查看ip地址： ipconfig</p>
<p>  ip地址的表示形式：点分十进制。(每个十进制的范围0~255)</p>
<p>  IPv4使用四个字节，IPv6使用十六个字节</p>
<p>  IP地址的组成 = 网络地址 + 主机地址</p>
<p>  IPv6是互联网工程任务组设计的用于替代IPv4的下一代IP协议，其地址数量号称可以为全世界任何一粒沙子编上一个地址。</p>
<p>  因为I于网络地址资源有限，严重制约了互联网的应用和发展，IPv6的使用，不仅能解决网络资源数量的问题，而且也解决了多种接入设备连入互联网的障碍。</p>
<p>特殊： 127.0.0.1</p>
<h4 id="域名"><a href="#域名" class="headerlink" title="域名"></a>域名</h4><p>  概念：将ip地址映射为域名。方面记忆，解决ip的记忆困难。</p>
<h4 id="端口号"><a href="#端口号" class="headerlink" title="端口号"></a>端口号</h4><p>  概念：用于标识计算器上某个特定的网络程序。</p>
<p>  表示形式：使用整数形式，范围0~65535(2个字节)</p>
<p>  0 ~ 1024已经被占用，比如ssh 22，ftp 21 ， smtp 25，http 80</p>
<h4 id="网络通信协议"><a href="#网络通信协议" class="headerlink" title="网络通信协议"></a>网络通信协议</h4><p>TCP/IP中文译名为传输控制协议/因特网互联协议，又叫网络通讯协议，这个协议是Internet最基本的协议，Internet国际互联网络的基础，简单的说，就是网络层的ip协议和传输层的TCP协议组成的。</p>
<p>TCP协议：传输控制协议</p>
<ol>
<li>使用TCP协议前，需要建立TCP链接，形成传输数据通道。</li>
<li>传输前，采用三次握手方式，是可靠的。</li>
<li>TCP协议进行通信的两个应用进程：客户端，服务器</li>
<li>在链接中可以进行大量数据的传输。</li>
<li>传输完毕，需要释放已经建立的链接，效率低。</li>
</ol>
<p>UDP协议：</p>
<ol>
<li><p>将数据，源，目的封装成数据包，==不需要建立链接==。</p>
</li>
<li><p>因为是无需连接的，所以是不可靠的。</p>
</li>
<li><p>每个数据报的大小限制在64k内。</p>
</li>
<li><p>发送数据结束后无需释放资源(因为不是面向链接的)，速度快。</p>
</li>
</ol>
<h4 id="InetAddress类"><a href="#InetAddress类" class="headerlink" title="InetAddress类"></a>InetAddress类</h4><p>  InetAddress类实现了Serializable接口所以可以实现串行化。</p>
<p>  获取InetAddress类对象：</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//获取当前主机的InetAddress类</span>
<span class="token class-name">InetAddress</span> localhost <span class="token operator">=</span> <span class="token class-name">InetAddress</span><span class="token punctuation">.</span><span class="token function">getLocalHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//获取指定域名的InetAddress类</span>
<span class="token class-name">InetAddress</span> localhost <span class="token operator">=</span> <span class="token class-name">InetAddress</span><span class="token punctuation">.</span><span class="token function">getByName</span><span class="token punctuation">(</span><span class="token string">"网址"</span><span class="token punctuation">)</span>
<span class="token comment">//获取指定主机名的InetAddress类</span>
<span class="token class-name">InetAddress</span> localhost <span class="token operator">=</span> <span class="token class-name">InetAddress</span><span class="token punctuation">.</span><span class="token function">getByName</span><span class="token punctuation">(</span><span class="token string">"主机名"</span><span class="token punctuation">)</span>
 <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>  方法：</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java">  
<span class="token comment">//通过  InetAddress对象获得ip地址</span>
localhost<span class="token punctuation">.</span><span class="token function">getHostAddress</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//通过  InetAddress对象获得域名</span>
localhost<span class="token punctuation">.</span><span class="token function">getHostName</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">//输出本机的ip地址和本机名</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>localhost<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="Socket"><a href="#Socket" class="headerlink" title="Socket"></a>Socket</h4><ol>
<li>套接字(Socket)开发网络应用程序被广泛采用，以至于称为事实上的标准。</li>
<li>通信的两端都要有Socket，这是两台机器间通信的端点。</li>
<li>网络通信其实就是socket间的通信。</li>
<li>socket允许程序把网络链接当成一个流，数据在两个socket间通过IO传输。</li>
<li>一般主动发起通信的应用程序属于客户端，等待通信请求的为服务端。 </li>
</ol>
<p><img src="./img/socket%E5%9B%BE%E8%A7%A3.png"></p>
<h4 id="TCP网络通信编程"><a href="#TCP网络通信编程" class="headerlink" title="TCP网络通信编程"></a>TCP网络通信编程</h4><p>  当客户端连接到服务端后，实际上也是通过一个端口和服务端进行通讯的，这个端口是TCP/IP来分配的，是不确定的，是随机的。</p>
<h5 id="TCP字节流编程"><a href="#TCP字节流编程" class="headerlink" title="TCP字节流编程"></a>TCP字节流编程</h5><p>  TCP字节流编程步骤：</p>
<p>  服务器：                                       </p>
<ol>
<li>设置监听端口获得ServerSocket对象。<ul>
<li><code>ServerSocket serverSocket = new ServerSocket(监听端口号);</code></li>
</ul>
</li>
<li>获得socket对象<ul>
<li><code>Socket socket = serverSocket.accept();</code>   //如果监听到数据会执行，如果没有阻塞。</li>
</ul>
</li>
<li>通过使用socket对象获得输入流，将数据通道的数据取出。<ul>
<li><code>InputStream inputStream = socket.getInputStream();</code></li>
<li><code>inputStream.read()</code></li>
</ul>
</li>
<li>通过使用socket对象获得输出流，像数据通道写入数据。<ul>
<li><code>OutputStream outputStream = socket.getOutputStream();</code></li>
<li><code>outputStream.write(字符)</code></li>
</ul>
</li>
<li>==设置写入结束标记==。<ul>
<li><code>socket.shutdownOutput();</code>   //如果不设置结束标记Socket对象会一直等待</li>
</ul>
</li>
<li>关闭输入输出流，socket对象，ServerSocket对象。<ul>
<li><code>outputStream.close();</code></li>
<li><code>inputStream.close();</code></li>
<li><code>socket.close();</code></li>
<li><code>serverSocket.close();</code></li>
</ul>
</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//服务器端</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//建立端口监听</span>
        <span class="token class-name">ServerSocket</span> serverSocket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ServerSocket</span><span class="token punctuation">(</span><span class="token number">9999</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"---------------服务器监听中……"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        <span class="token class-name">Socket</span> socket <span class="token operator">=</span> serverSocket<span class="token punctuation">.</span><span class="token function">accept</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"----------------服务器客户端通道建立"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        <span class="token comment">//使用cocket对象向数据通道读取数据</span>
        <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> socket<span class="token punctuation">.</span><span class="token function">getInputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> readLen <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> by <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>readLen <span class="token operator">=</span> inputStream<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>by<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>by<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>readLen<span class="token punctuation">)</span>；
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"服务器得到数据："</span> <span class="token operator">+</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        
 
 
        <span class="token comment">//使用cocket对象向数据通道写入数据</span>
        <span class="token class-name">OutputStream</span> outputStream <span class="token operator">=</span> socket<span class="token punctuation">.</span><span class="token function">getOutputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        outputStream<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"$$$$服务器向客户端返回的数据"</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		socket<span class="token punctuation">.</span><span class="token function">shutdownOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-----------------客户端返回数据"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
 
 
        <span class="token comment">//关闭流和socket</span>
        outputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        inputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        socket<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        serverSocket<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
 
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>  客户端：</p>
<ol>
<li>连接指定InetAddress的服务器，设置端口。获得Socket对象<ul>
<li><code>Socket socket = new Socket(InetAddress对象, 端口);</code></li>
</ul>
</li>
<li>通过使用socket对象获得输出流，像数据通道写入数据。<ul>
<li><code>OutputStream outputStream = socket.getOutputStream();</code></li>
<li><code>outputStream.write(字符)</code></li>
</ul>
</li>
<li>设置写入结束标记。<ul>
<li><code>socket.shutdownOutput();</code>   //如果不设置结束标记Socket对象会一直等待</li>
</ul>
</li>
<li>通过使用socket对象获得输入流，将数据通道的数据取出。<ul>
<li><code>InputStream inputStream = socket.getInputStream();</code></li>
<li><code>inputStream.read()</code></li>
</ul>
</li>
<li>关闭输入输出流，socket对象。<ul>
<li><code>outputStream.close();</code></li>
<li><code>inputStream.close();</code></li>
<li><code>socket.close();</code></li>
</ul>
</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//客户端</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//连接指定服务器的指定端口</span>
        <span class="token class-name">Socket</span> socket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Socket</span><span class="token punctuation">(</span><span class="token class-name">InetAddress</span><span class="token punctuation">.</span><span class="token function">getLocalHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">9999</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        <span class="token comment">//使用socket对象向数据通道中写入数据(字节流)</span>
        <span class="token class-name">OutputStream</span> outputStream <span class="token operator">=</span> socket<span class="token punctuation">.</span><span class="token function">getOutputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       
        outputStream<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"￥￥￥￥客户端发送的数据"</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      
        socket<span class="token punctuation">.</span><span class="token function">shutdownOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
       
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"--------------客户端向服务器发送数据"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
 
        <span class="token comment">//使用socket对象向数据通道中读取数据(字符流)</span>
        <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> socket<span class="token punctuation">.</span><span class="token function">getInputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       
        <span class="token keyword">int</span> readLen <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> by <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>readLen <span class="token operator">=</span> inputStream<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>by<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>by<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>readLen<span class="token punctuation">)</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"服务器返回的数据："</span> <span class="token operator">+</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
       
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-------------------客户端返回数据"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//关闭流也socket对象</span>
        inputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        outputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bufferedReader<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        socket<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="TCP字符流编程"><a href="#TCP字符流编程" class="headerlink" title="TCP字符流编程"></a>TCP字符流编程</h5><p>  TCP字符流编程步骤：</p>
<p>  服务器：                                       </p>
<ol>
<li>设置监听端口获得ServerSocket对象。<ul>
<li><code>ServerSocket serverSocket = new ServerSocket(监听端口号);</code></li>
</ul>
</li>
<li>获得socket对象<ul>
<li><code>Socket socket = serverSocket.accept();</code></li>
</ul>
</li>
<li>通过使用socket对象获得输入流，将数据通道的数据取出，使用转换流和处理流得到相应的字符流。<ul>
<li><code>InputStream inputStream = socket.getInputStream();</code></li>
<li><code>BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); </code>   //使用转换流和处理流</li>
<li><code>bufferedReader.read()</code></li>
</ul>
</li>
<li>通过使用socket对象获得输出流，像数据通道写入数据，使用转换流和处理流得到相应的字符流。<ul>
<li><code>OutputStream outputStream = socket.getOutputStream();</code></li>
<li><code>BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));</code>     //使用转换流和处理流</li>
<li><code>bufferedWriter.write(字符)</code></li>
</ul>
</li>
<li>设置写入结束标记,刷新流。<ul>
<li><code>bufferedWriter.newLine();</code>   //如果不设置结束标记Socket对象会一直等待</li>
<li><code>bufferedWriter.flush();</code>    //如果不设置刷新则不会写入数据</li>
</ul>
</li>
<li>关闭输入输出流，socket对象，ServerSocket对象。<ul>
<li><code>bufferedReader.close();</code></li>
<li><code>bufferedWriter.close();</code></li>
<li><code>socket.close();</code></li>
<li><code>serverSocket.close();</code></li>
</ul>
</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//服务器端</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//建立端口监听</span>
        <span class="token class-name">ServerSocket</span> serverSocket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ServerSocket</span><span class="token punctuation">(</span><span class="token number">9999</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"---------------服务器监听中……"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        <span class="token class-name">Socket</span> socket <span class="token operator">=</span> serverSocket<span class="token punctuation">.</span><span class="token function">accept</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"----------------服务器客户端通道建立"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        <span class="token comment">//使用cocket对象向数据通道读取数据</span>
        <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> socket<span class="token punctuation">.</span><span class="token function">getInputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">BufferedReader</span> bufferedReader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InputStreamReader</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span> bufferedReader<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"服务器得到数据："</span> <span class="token operator">+</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
 
 
        <span class="token comment">//使用cocket对象向数据通道写入数据</span>
        <span class="token class-name">OutputStream</span> outputStream <span class="token operator">=</span> socket<span class="token punctuation">.</span><span class="token function">getOutputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">BufferedWriter</span> bufferedWriter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedWriter</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OutputStreamWriter</span><span class="token punctuation">(</span>outputStream<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bufferedWriter<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"$$$$服务器向客户端返回的数据"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bufferedWriter<span class="token punctuation">.</span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bufferedWriter<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-----------------客户端返回数据"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
 
 
        <span class="token comment">//关闭流和socket</span>
        bufferedReader<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bufferedWriter<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        socket<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        serverSocket<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
 
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>  客户端：</p>
<ol>
<li>连接指定InetAddress的服务器，设置端口。获得Socket对象<ul>
<li><code>Socket socket = new Socket(InetAddress对象, 端口);</code></li>
</ul>
</li>
<li>通过使用socket对象获得输出流，像数据通道写入数据，使用转换流和处理流得到相应的字符流。<ul>
<li><code>OutputStream outputStream = socket.getOutputStream();</code></li>
<li><code>BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));</code>     //使用转换流和处理流</li>
<li><code>bufferedWriter.write(字符)</code></li>
</ul>
</li>
<li>设置写入结束标记,刷新流。<ul>
<li><code>bufferedWriter.newLine();</code>   //如果不设置结束标记Socket对象会一直等待</li>
<li><code>bufferedWriter.flush();</code>    //如果不设置刷新则不会写入数据</li>
</ul>
</li>
<li>通过使用socket对象获得输入流，将数据通道的数据取出，使用转换流和处理流得到相应的字符流。<ul>
<li><code>InputStream inputStream = socket.getInputStream();</code></li>
<li><code>BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); </code>   //使用转换流和处理流</li>
<li><code>bufferedReader.read()</code></li>
</ul>
</li>
<li>关闭输入输出流，socket对象。<ul>
<li><code>bufferedReader.close();</code></li>
<li><code>bufferedWriter.close();</code></li>
<li><code>socket.close();</code></li>
</ul>
</li>
</ol>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//客户端</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//连接指定服务器的指定端口</span>
        <span class="token class-name">Socket</span> socket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Socket</span><span class="token punctuation">(</span><span class="token class-name">InetAddress</span><span class="token punctuation">.</span><span class="token function">getLocalHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">9999</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        <span class="token comment">//使用socket对象向数据通道中写入数据(字符流)</span>
        <span class="token class-name">OutputStream</span> outputStream <span class="token operator">=</span> socket<span class="token punctuation">.</span><span class="token function">getOutputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">BufferedWriter</span> bufferedWriter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedWriter</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OutputStreamWriter</span><span class="token punctuation">(</span>outputStream<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bufferedWriter<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"￥￥￥￥客户端发送的数据"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//设置写入数据结束标记</span>
        bufferedWriter<span class="token punctuation">.</span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//刷新数据，如果不刷新不会将指定数据写入通道</span>
        bufferedWriter<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"--------------客户端向服务器发送数据"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
 
        <span class="token comment">//使用socket对象向数据通道中读取数据(字符流)</span>
        <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> socket<span class="token punctuation">.</span><span class="token function">getInputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">BufferedReader</span> bufferedReader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InputStreamReader</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span> bufferedReader<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"服务器返回的数据："</span> <span class="token operator">+</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-------------------客户端返回数据"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
 
        <span class="token comment">//关闭流与socket对象</span>
 
        bufferedWriter<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bufferedReader<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        socket<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
 
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="netstat指令"><a href="#netstat指令" class="headerlink" title="netstat指令"></a>netstat指令</h4><ol>
<li>netstat - an 可以查到当前主机网络情况，包括端口监听情况和网络连接情况。</li>
<li>netstat - an | more 可以分页显示。</li>
<li>要求在dos控制台下执行。 </li>
</ol>
<p><img src="./img/netstat.png"></p>
<ul>
<li>LISTENING表示某个端口在监听。</li>
<li>如果有一个外部程序(客户端)连接到该端口，就会显示一天连接信息。</li>
</ul>
<h4 id="UDP网络通信编程"><a href="#UDP网络通信编程" class="headerlink" title="UDP网络通信编程"></a>UDP网络通信编程</h4><ol>
<li>类DatagramSocket和DatagramPacket 实现了基于UDP协议网络程序。</li>
<li>UDP数据报通过数据报套接字DatagramSocket发送和接收，系统不保证UDP数据报一定能够安全送到目的地，也不能确定什么时候可以到达。</li>
<li>DatagramPacket对象封装了UDP数据报，在数据报中包含了发送端的ip地址和端口号以及接收端的ip地址和端口号。</li>
<li>UDP协议中每个数据报都给出了完整的地址信息，因此无需建立发送方和接收方的来连接。</li>
</ol>
<p>说明：</p>
<ul>
<li><p>没有明确的服务端和客户端，演变成数据的发送端和接收端。</p>
</li>
<li><p>接收数据和发送数据是通过DatagramSocket对象完成。</p>
</li>
<li><p>将数据封装到DatagramPacket对象(装包)。</p>
</li>
<li><p>当接收到DatagramPacket对象，需要进行拆包，取出数据。</p>
</li>
<li><p>DatagramSocket可以指定在那个端口接收数据。</p>
</li>
<li><p>接收端和服务端都可以设定使用的接口。</p>
<p>构造器：</p>
<ul>
<li><p>DatagramSocket(端口号)；</p>
</li>
<li><p>DatagramPacket(数据, 数据长度,接收端InetAddress对象,端口号);   //用于发送数据</p>
</li>
<li><p>DatagramPacket(数据, 数据长度);  //用于接收数据</p>
<p>UDP编程步骤：</p>
</li>
</ul>
<ol>
<li>创建DatagramSocket对象，在指定端口等待接收数据<ul>
<li><code>DatagramSocket socket = new DatagramSocket(9998);</code></li>
</ul>
</li>
<li>将需要发送的数据封装为DatagramPacket对象。<ul>
<li><code>byte[] data = &quot;发送端封装的数据&quot;.getBytes();</code></li>
<li><code>DatagramPacket packet = new DatagramPacket(data, data.length,InetAddress.getLocalHost(),9999);</code> //DatagramPacket(数据,数据长度,IP地址对象,端口);</li>
</ul>
</li>
<li>发送数据。<ul>
<li><code>socket.send(packet);</code></li>
</ul>
</li>
<li>构造接收数据的DatagramPacket对象。<ul>
<li><code>byte[] datas = new byte[64*1024];</code></li>
<li><code>DatagramPacket repacket = new DatagramPacket(datas, datas.length);</code> //DatagramPacket(数据,数据长度);</li>
</ul>
</li>
<li>接收数据。<ul>
<li><code>socket.receive(repacket);</code></li>
</ul>
</li>
<li>将接收的数据报拆包。<ul>
<li><code>int length = repacket.getLength();</code></li>
<li><code>byte[] redata = repacket.getData();</code></li>
<li><code>String str = new String(redata,0,length);</code></li>
</ul>
</li>
<li>关闭资源<ul>
<li><code>socket.close();</code></li>
</ul>
</li>
</ol>
</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>

        <span class="token comment">//1.创建DatagramSocket对象准备返回和接收数据</span>
        <span class="token comment">//准备在指定端口接收数据</span>
        <span class="token class-name">DatagramSocket</span> socket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DatagramSocket</span><span class="token punctuation">(</span><span class="token number">9998</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//2.将需要发送的数据封装到DatagramPacket</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> data <span class="token operator">=</span> <span class="token string">"发送端封装的数据"</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//DatagramPacket(数据,数据长度,IP地址,端口);</span>
        <span class="token class-name">DatagramPacket</span> packet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DatagramPacket</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> data<span class="token punctuation">.</span>length<span class="token punctuation">,</span><span class="token class-name">InetAddress</span><span class="token punctuation">.</span><span class="token function">getLocalHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">9999</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//4.发送数据</span>
        socket<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>packet<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//----------------------------------</span>
        <span class="token comment">//5.构造接收datagramPacket对象</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> datas <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">64</span><span class="token operator">*</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token class-name">DatagramPacket</span> repacket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DatagramPacket</span><span class="token punctuation">(</span>datas<span class="token punctuation">,</span> datas<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//6.接收数据</span>
        socket<span class="token punctuation">.</span><span class="token function">receive</span><span class="token punctuation">(</span>repacket<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"接收到数据……"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//7.拆包</span>
        <span class="token keyword">int</span> length <span class="token operator">=</span> repacket<span class="token punctuation">.</span><span class="token function">getLength</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> redata <span class="token operator">=</span> repacket<span class="token punctuation">.</span><span class="token function">getData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>redata<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>



        <span class="token comment">//8.关闭资源</span>
        socket<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"B端退出……"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>  先接收端</p>
<ol>
<li>创建DatagramSocket对象，在指定端口等待接收数据<ul>
<li><code>DatagramSocket socket = new DatagramSocket(9998);</code></li>
</ul>
</li>
<li>构造接收数据的DatagramPacket对象。<ul>
<li><code>byte[] datas = new byte[64*1024];</code></li>
<li><code>DatagramPacket repacket = new DatagramPacket(datas, data.length);</code> //DatagramPacket(数据,数据长度);</li>
</ul>
</li>
<li>接收数据。<ul>
<li><code>socket.receive(repacket);</code></li>
</ul>
</li>
<li>将接收的数据报拆包。<ul>
<li><code>int length = repacket.getLength();</code></li>
<li><code>byte[] redata = repacket.getData();</code></li>
<li><code>String str = new String(redata,0,length);</code></li>
</ul>
</li>
<li>将需要发送的数据封装为DatagramPacket对象。<ul>
<li><code>byte[] data = &quot;发送端封装的数据&quot;.getBytes();</code></li>
<li><code>DatagramPacket packet = new DatagramPacket(data, data.length,InetAddress.getLocalHost(),9999);</code> //DatagramPacket(数据,数据长度,IP地址,端口);</li>
</ul>
</li>
<li>发送数据。<ul>
<li><code>socket.send(packet);</code></li>
</ul>
</li>
<li>关闭资源<ul>
<li><code>socket.close();</code></li>
</ul>
</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//1.创建一个DatagramSocket对象，准备在指定端口接收数据，</span>
        <span class="token class-name">DatagramSocket</span> socket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DatagramSocket</span><span class="token punctuation">(</span><span class="token number">9999</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//2.构建一个DatagramPacket对象，准备接收数据.</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">64</span> <span class="token operator">*</span> <span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token class-name">DatagramPacket</span> packet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DatagramPacket</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span>buf<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//3.调用接收方法。将通过网络传输的DatagramPacket对象填充到packet对象中。</span>
        <span class="token comment">//当有数据报发送到本机指定端口时，会接收数据。如果没有数据发送到指定端口，就会阻塞等待。</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"正在9999端口等待数据到来……"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        socket<span class="token punctuation">.</span><span class="token function">receive</span><span class="token punctuation">(</span>packet<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"接收到数据进行拆包……"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//4.将packet进行拆包，取出数据，并显示</span>
        <span class="token keyword">int</span> length <span class="token operator">=</span> packet<span class="token punctuation">.</span><span class="token function">getLength</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//实际接收到的数据长度</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> data <span class="token operator">=</span> packet<span class="token punctuation">.</span><span class="token function">getData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//接收到数据</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
                
        <span class="token comment">//--------------------------</span>
        <span class="token comment">//5.创建datagrampacketu对象封装数据。</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> rebuf <span class="token operator">=</span> <span class="token string">"重新发送的数据"</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">DatagramPacket</span> packets <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DatagramPacket</span><span class="token punctuation">(</span>rebuf<span class="token punctuation">,</span> rebuf<span class="token punctuation">.</span>length<span class="token punctuation">,</span> <span class="token class-name">InetAddress</span><span class="token punctuation">.</span><span class="token function">getLocalHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">9998</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//6.发送数据</span>
        socket<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>packets<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//7.关闭资源</span>
        socket<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"A端退出……"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<h3 id="50-项目开发流程"><a href="#50-项目开发流程" class="headerlink" title="50. 项目开发流程"></a>50. 项目开发流程</h3><blockquote>
<p> 需求分析 - 设计阶段 - 实现阶段 - 测试阶段 - 实施阶段 - 维护阶段</p>
</blockquote>
<p><img src="./img/%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91%E6%B5%81%E7%A8%8B%E5%9B%BE.png"></p>
<h3 id="51-反射"><a href="#51-反射" class="headerlink" title="51. 反射"></a>51. 反射</h3><p>  通过外部文件配置，在不修改源码情况下，来控制程序，也符合设计模式ocp(开闭原则：不修改源码，扩容功能)</p>
<p>  优点：</p>
<ul>
<li>可以动态的创建和使用对象(是框架底层核心)，使用灵活，没有反射机制，框架技术就失去底层支撑。</li>
</ul>
<p>  缺点：</p>
<ul>
<li>使用反射基本是解释执行，对执行速度有影响。</li>
</ul>
<h4 id="反射机制-java-reflection-："><a href="#反射机制-java-reflection-：" class="headerlink" title="反射机制(java reflection)："></a>反射机制(java reflection)：</h4><blockquote>
<p> 反射机制允许程序在执行期借助于reflectionAPI取得任何类的内部信息(比如成员变量，构造器，成员方法等)，并能操作对象的属性以及方法。反射在设计模式和框架底层都会用到。</p>
</blockquote>
<p>  加载完类之后，在堆中就产生了一个Class类型的对象(一个类只有一个CLass对象)，这个对象包含了类的完整结构信息，通过这个对象得到类的结构。这个对象就像一面镜子，透过这个镜子看到类的结构，所以称之为：反射。</p>
<ul>
<li>在运行时判断任意一个对象所属的类。</li>
<li>在运行时构造任意一个类的对象。</li>
<li>在运行时得到任意一个类的所育有的成员变量的方法。</li>
<li>在运行时调用任意一个对象的成员变量和方法。</li>
<li>生成动态代理。</li>
</ul>
<p><img src="./img/%E5%8F%8D%E5%B0%84%E6%9C%BA%E5%88%B6.png" alt="反射机制图"></p>
<h4 id="反射机制相关类"><a href="#反射机制相关类" class="headerlink" title="反射机制相关类"></a>反射机制相关类</h4><p>  java.lang.Class  代表一个类，Class对象表示某个类加载后在堆中的对象。</p>
<p>  java.lang.reflect.Method 代表类的方法 </p>
<p>  java.lang.reflect.Field  代表类的成员变量</p>
<p>  java.lang.reflect.Constructor 代表类的构造方法</p>
<h4 id="反射操作对象"><a href="#反射操作对象" class="headerlink" title="反射操作对象"></a>反射操作对象</h4><ol>
<li><p>使用Properties类操作配置文件，取出类的路径和方法名，属性名</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Properties</span> pr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
pr<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\re.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> classname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"ClassName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> methodname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"MethodName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> fieldname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"FieldName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
<li><p>使用类的路径名，类名，构造出相应类的Class对象。</p>
<ul>
<li><code>Class cls = Class.forName(classname);</code></li>
</ul>
</li>
<li><p>通过Class类的对象加载类的实例</p>
<ul>
<li><code>Object o = cls.newInstance();</code></li>
</ul>
</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">,</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">IllegalAccessException</span><span class="token punctuation">,</span> <span class="token class-name">InstantiationException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchMethodException</span><span class="token punctuation">,</span> <span class="token class-name">InvocationTargetException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchFieldException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//构造Properties类获取配置文件中的属性</span>
   		<span class="token class-name">Properties</span> pr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pr<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\re.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> classname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"Username"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//加载类，返回Class类型</span>
        <span class="token class-name">Class</span> cls <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>classname<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//通过cls得到加载类的实例</span>
        <span class="token class-name">Object</span> o <span class="token operator">=</span> cls<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"o的运行类型为="</span> <span class="token operator">+</span> o<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>
<span class="token class-name">Username</span><span class="token operator">=</span><span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>qn<span class="token punctuation">.</span>resd<span class="token punctuation">.</span></span>DouBao</span>
method<span class="token operator">=</span>cry<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="反射操作方法"><a href="#反射操作方法" class="headerlink" title="反射操作方法"></a>反射操作方法</h4><ol>
<li><p>使用Properties类操作配置文件，取出类的路径和方法名，属性名</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Properties</span> pr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
pr<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\re.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> classname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"ClassName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> methodname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"MethodName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> fieldname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"FieldName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
<li><p>使用类的路径名，类名，构造出相应类的Class对象。</p>
<ul>
<li><code>Class cls = Class.forName(classname);</code></li>
</ul>
</li>
<li><p>通过Class类的对象加载类的实例</p>
<ul>
<li><code>Object object = cls.newInstance();</code></li>
</ul>
</li>
<li><p>通过Class类的对象加载对应方法对象</p>
<ul>
<li><code>Method cryMethod = cls.getMethod(cry);</code></li>
</ul>
</li>
<li><p>通过Method的对象调用方法，传入方法对应的实例对象</p>
<ul>
<li><code>cryMethod.invoke(object);</code></li>
</ul>
</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">,</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">IllegalAccessException</span><span class="token punctuation">,</span> <span class="token class-name">InstantiationException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchMethodException</span><span class="token punctuation">,</span> <span class="token class-name">InvocationTargetException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchFieldException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//构造Properties类获取配置文件中的属性</span>
   		<span class="token class-name">Properties</span> pr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pr<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\re.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> classname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"Username"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//加载类，返回Class类型</span>
        <span class="token class-name">Class</span> cls <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>classname<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//通过cls得到加载类的实例</span>
        <span class="token class-name">Object</span> o <span class="token operator">=</span> cls<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"o的运行类型为="</span> <span class="token operator">+</span> o<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  		<span class="token comment">//获得对应方法的对象</span>
  		<span class="token class-name">Method</span> cryMethod <span class="token operator">=</span> cls<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span>cry<span class="token punctuation">)</span><span class="token punctuation">;</span>
  		<span class="token comment">//调用方法  </span>
  		cryMethod<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">;</span>
  
    <span class="token punctuation">&#125;</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>
<span class="token class-name">Username</span><span class="token operator">=</span><span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>qn<span class="token punctuation">.</span>resd<span class="token punctuation">.</span></span>DouBao</span>
method<span class="token operator">=</span>cry<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="反射操作属性"><a href="#反射操作属性" class="headerlink" title="反射操作属性"></a>反射操作属性</h4><p>  无法访问私有属性。</p>
<ol>
<li><p>使用Properties类操作配置文件，取出类的路径和方法名，属性名</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Properties</span> pr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
pr<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\re.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> classname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"ClassName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> methodname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"MethodName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> fieldname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"FieldName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
<li><p>使用类的路径名，类名，构造出相应类的Class对象。</p>
<ul>
<li><code>Class cls = Class.forName(classname);</code></li>
</ul>
</li>
<li><p>通过Class类的对象加载类的实例</p>
<ul>
<li><code>Object object = cls.newInstance();</code></li>
</ul>
</li>
<li><p>通过Class类的对象加载对应属性对象</p>
<ul>
<li><code>Field fieldname = cls.getField(&quot;fieldname&quot;);</code></li>
</ul>
</li>
<li><p>通过Field的对象调用方法，传入方法对应的实例对象</p>
<ul>
<li><code>fieldname.get(object);</code></li>
</ul>
</li>
<li><p>通过Field对象更改属性值</p>
<ul>
<li><code>fieldname.get(object,&quot;值&quot;);</code></li>
</ul>
</li>
<li><p>遍历所有的属性</p>
<ul>
<li><code>Field[] fieldname = cls.getFields();</code></li>
<li><code>for(Field f : filelds)&#123; f.getName()&#125;</code></li>
</ul>
</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">,</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">IllegalAccessException</span><span class="token punctuation">,</span> <span class="token class-name">InstantiationException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchMethodException</span><span class="token punctuation">,</span> <span class="token class-name">InvocationTargetException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchFieldException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//构造Properties类获取配置文件中的属性</span>
   		<span class="token class-name">Properties</span> pr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pr<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\re.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> classname <span class="token operator">=</span> pr<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"Username"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//加载类，返回Class类型</span>
        <span class="token class-name">Class</span> cls <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>classname<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//通过cls得到加载类的实例</span>
        <span class="token class-name">Object</span> o <span class="token operator">=</span> cls<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"o的运行类型为="</span> <span class="token operator">+</span> o<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  		<span class="token comment">//通过Class类的对象加载对应属性对象</span>
  		<span class="token class-name">Field</span> fieldname <span class="token operator">=</span> cls<span class="token punctuation">.</span><span class="token function">getField</span><span class="token punctuation">(</span><span class="token string">"fieldname"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  		<span class="token comment">//通过Method的对象调用方法，传入方法对应的实例对象</span>
  		fieldname<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>
<span class="token class-name">Username</span><span class="token operator">=</span><span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>qn<span class="token punctuation">.</span>resd<span class="token punctuation">.</span></span>DouBao</span>
method<span class="token operator">=</span>cry<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="反射机制优化"><a href="#反射机制优化" class="headerlink" title="反射机制优化"></a>反射机制优化</h4><p>  Method和Field,Constructor对象都有setAccessible()方法。</p>
<p>  setAccessible作用是启动和禁用访问安全检查的开关。</p>
<p>  参数值为true表示反射的对象在使用时取消访问检查，提高反射的效率，参数值未false则表示对反射执行访问检查。(但是效果并不明显)</p>
<h4 id="Class类"><a href="#Class类" class="headerlink" title="Class类"></a>Class类</h4><p>  Class类继承Object类。</p>
<p>  ==Class类对象不是new出来的，而是系统创建的。==</p>
<p>  对于每个类的Class对象，在内存中只有一份，因为类只加载一次。</p>
<p>  每个类的实例都会记的自己是哪个Class实例所生成。</p>
<p>  通过Class可以完整的得到一个类的完整的结构，通过一系列API。</p>
<p>  Class对象是存放在堆中的。</p>
<p>  类的字节码二进制数据，是放在方法区的，有的地方称为类的元数据。</p>
<p>  如下类型有Class对象</p>
<ol>
<li>外部类，成员内部类，静态内部类，局部内部类，匿名内部类</li>
<li>interface ：接口</li>
<li>数组</li>
<li>enum ： 枚举</li>
<li>annottation ： 注解</li>
<li>基本数据类型</li>
<li>void</li>
</ol>
<p>  获取Class类对象</p>
<ol>
<li><p>前提：已知一个类的全类名，且该类在类路径下，可通过Class类的静态方法fotName()获取，可能抛出ClassNotFoundExcepotion </p>
<ul>
<li>多用于配置文件</li>
</ul>
<p><code>Class cls = Class.forName(&quot;java.long.Cat&quot;)</code></p>
</li>
<li><p>前提：已知具体的类，通过类名.class获取，该方式最为安全可靠，程序性能最高。</p>
<ul>
<li>多用于参数传递</li>
</ul>
<p><code>Class cls = Cat.class</code></p>
</li>
<li><p>前提：一直某个类的实例，调用该实例的getClass()方法获取Class对象。也是获得运行类型的方法。</p>
<p><code>Class cls = 对象.getClass();</code></p>
</li>
<li><p>通过类加载器获得类的Class的对象</p>
<p><code>Class cls = classLoader.loadClass(classAllPath)</code></p>
</li>
<li><p>基本数据类型(int,double,char,boolean,long,float,byte,short)可以通过 基本数据类型.class 得到对应的Class类对象。</p>
<p><code>Class cls = 基本数据类型.class</code></p>
</li>
<li><p>基本数据类型对应的包装类，可以通过 包装类.TYPE得到对应的Class类对象</p>
<p><code>Class cls = 包装类.TYPE</code></p>
<p>5和6得到的Class类相同</p>
</li>
</ol>
<p>  常用方法</p>
<table>
<thead>
<tr>
<th>方法名</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>static Class forName(String name)</td>
<td>返回指定类名的class对象</td>
</tr>
<tr>
<td>getName（）</td>
<td>获取全类名(类名+包名)</td>
</tr>
<tr>
<td>getSimpleName()</td>
<td>获取简单类名(类名)</td>
</tr>
<tr>
<td>getField()</td>
<td>返回所有public修饰的属性，包含本类以及父类的</td>
</tr>
<tr>
<td>getDeclaredField()</td>
<td>返回本类中的所有属性</td>
</tr>
<tr>
<td>getMethod()</td>
<td>获取所有public修饰的方法，包含本类以及父类</td>
</tr>
<tr>
<td>getDeclaredMethod()</td>
<td>获取本类中的所有方法。</td>
</tr>
<tr>
<td>getConstructor()</td>
<td>返回一个包含本类public修饰Constructor对象的数组</td>
</tr>
<tr>
<td>getDeclaredConstructor()</td>
<td>获取本类中的所有构造器</td>
</tr>
<tr>
<td>getPackage()</td>
<td>以package得到类的包名</td>
</tr>
<tr>
<td>Class getSuperClass()</td>
<td>返回当前Class对象的父类Class对象</td>
</tr>
<tr>
<td>Class [] getInterface()</td>
<td>返回当前Class对象的接口</td>
</tr>
<tr>
<td>ClassLoader getClassLoader()</td>
<td>返回该类的类加载器</td>
</tr>
<tr>
<td>Class getSuperclass()</td>
<td>返回此Class所表示实体的超类的Class</td>
</tr>
<tr>
<td>getAnnontations()</td>
<td>以Annotation[] 形式返回注释信息</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody></table>
<h4 id="Field类"><a href="#Field类" class="headerlink" title="Field类"></a>Field类</h4><p>  常用方法：</p>
<ul>
<li>getModifiers()：以int形式返回修饰符<ul>
<li>默认修饰符为 0；public 为 1；private为2;protected为4</li>
<li>static 为8；final为 16</li>
<li>多个修饰符返回相加的值</li>
</ul>
</li>
<li>getType()以Class形式返回类型</li>
<li>getName() 返回属性名</li>
</ul>
<h4 id="Method类"><a href="#Method类" class="headerlink" title="Method类"></a>Method类</h4><p>  常用方法：</p>
<ul>
<li>getModifiers()以int形式返回修饰符<ul>
<li>默认修饰符为 0；public 为 1；private为2;protected为4</li>
<li>static 为8；final为 16</li>
<li>多个修饰符返回相加的值</li>
</ul>
</li>
<li>getRetrunType() : 以Class形式获取返回类型</li>
<li>getName() ： 返回方法名</li>
<li>getParameterType() ： 以Class[]返回参数类型数组</li>
</ul>
<h4 id="Constructor类"><a href="#Constructor类" class="headerlink" title="Constructor类"></a>Constructor类</h4><p>  常用方法：</p>
<ul>
<li>getModifiers()以int形式返回修饰符<ul>
<li>默认修饰符为 0；public 为 1；private为2;protected为4</li>
<li>static 为8；final为 16</li>
<li>多个修饰符返回相加的值</li>
</ul>
</li>
<li>getName() ： 返回构造器名(全类名)</li>
<li>getParameterTypes (): 以Class[]返回参数类型数组</li>
</ul>
<h4 id="类加载过程"><a href="#类加载过程" class="headerlink" title="类加载过程"></a>类加载过程</h4><p>  反射机制是java动态语言的关键，也就是通过反射实现类的动态加载。</p>
<ol>
<li>静态加载：编译时加载相关的类，如果没有则报错，依赖性太强。</li>
<li>动态加载：运行时加载需要的类，如果运行时不用该类，则不报错，降低了依赖性。</li>
</ol>
<p>  类加载时机：</p>
<ul>
<li>当创建对象时(new)         ————–静态加载</li>
<li>当子类被加载时，父类加载       ————–静态加载</li>
<li>调用类中的静态成员时   ————–静态加载</li>
<li>通过反射     ——-动态加载</li>
</ul>
<h4 id=""><a href="#" class="headerlink" title=""></a></h4><h4 id="1-类加载"><a href="#1-类加载" class="headerlink" title="1. 类加载"></a>1. 类加载</h4><pre><code>JVM在该阶段的主要目的是将字节码从不同的数据源(可能是class文件，也可能是jar包，甚至网络)转化二进制字节流加载到内存中，并生成一个代表类的java.lang.Class对象。
</code></pre>
<h4 id="2-连接阶段-验证"><a href="#2-连接阶段-验证" class="headerlink" title="2. 连接阶段-验证"></a>2. 连接阶段-验证</h4><ol>
<li>目的是为了确保Class文件的字节流中包含的信息符合当前虚拟机的要求，并且不会危害虚拟机自身的安全。</li>
<li>包括：文件格式验证(是否以obcafebabe开头)，元数据验证，字节码验证和符号引用验证。</li>
<li>可以考虑使用——Xverify：none参数来关闭大部分的类验证措施，缩短虚拟机类加载的时间。</li>
</ol>
<h4 id="3-连接阶段-准备"><a href="#3-连接阶段-准备" class="headerlink" title="3.连接阶段-准备"></a>3.连接阶段-准备</h4><ol>
<li>JVM会在该阶段对静态变量，匹配内存并==默认初始化==(对应数据类型的默认初始值，如0,0L,null,false)。这些变量所使用的内存都将在方法区中进行分配。</li>
<li>只有静态变量才会分配内存，默认初始化。</li>
<li>static final的常量，它和静态变量不同，因为一旦赋值就不变，所以会直接赋值。</li>
</ol>
<h4 id="3-连接阶段-解析"><a href="#3-连接阶段-解析" class="headerlink" title="3.连接阶段-解析"></a>3.连接阶段-解析</h4><p>   虚拟机将常量池中的符号引用替换为直接引用的过程。</p>
<h4 id="4-初始化"><a href="#4-初始化" class="headerlink" title="4. 初始化"></a>4. 初始化</h4><ol>
<li>到初始化阶段，才是真正开始执行类中定义的java程序代码。此阶段是执行&lt;clinit ()方法的过程。</li>
<li>&lt;clinit ()方法是由编译器按语句在源文件中出现的顺序，一次自动收集类中的所有静态变量的赋值动作和静态代码块中的语句，并进行合并。</li>
<li>虚拟机会保证一个类&lt;clinit ()方法在多线程环境中被正确的加锁，同步，如果多个线程同时去初始化一个类，那么只有一个线程回去执行这个类的&lt;clinit ()方法，其他线程都会被阻塞等待，直到活动线程执行&lt;clinit ()方法完成。</li>
</ol>
<h4 id="通过反射创建对象"><a href="#通过反射创建对象" class="headerlink" title="通过反射创建对象"></a>通过反射创建对象</h4><p>  方式一：调用类中的public修饰的无参构造器</p>
<p>  方式二：调用类中的指定构造器</p>
<p>  相关方法：</p>
<p>  Class类：</p>
<ul>
<li>newInstance() : 调用类中的无参构造器，获取对应的对象。</li>
<li>getConstructor(Class class)根据参数列表，获取对应的public构造器对象</li>
<li>getDecalaredConstructor(Class class):根据参数列表，获取对应的所有构造器对象。</li>
</ul>
<p>  Constructor类相关方法。</p>
<ul>
<li>setAccessible     //爆破，使用反射可以访问private的构造器/方法/属性</li>
<li>newInstance(Object obj):调用构造器</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">,</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">IllegalAccessException</span><span class="token punctuation">,</span> <span class="token class-name">InstantiationException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchMethodException</span><span class="token punctuation">,</span> <span class="token class-name">InvocationTargetException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"com.qn.xc.User"</span><span class="token punctuation">;</span>
        <span class="token comment">//1.获取到指定类的Class对象</span>
        <span class="token class-name">Class</span><span class="token operator">&lt;</span><span class="token operator">?</span>  aClass <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//2.通过public的无参构造器创建实例</span>
        <span class="token class-name">Object</span> o <span class="token operator">=</span> aClass<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//3.通过public的有参构造器创建实例</span>
        <span class="token comment">//先得到对应的构造器，再创建实例</span>
        <span class="token class-name">Constructor</span><span class="token operator">&lt;</span><span class="token operator">?</span>  constructor <span class="token operator">=</span> aClass<span class="token punctuation">.</span><span class="token function">getConstructor</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Object</span> o1 <span class="token operator">=</span> constructor<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token string">"实参"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>o1<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//4.通过非public的有参构造器创建实例</span>
        <span class="token class-name">Constructor</span><span class="token operator">&lt;</span><span class="token operator">?</span>  constructor1 <span class="token operator">=</span> aClass<span class="token punctuation">.</span><span class="token function">getDeclaredConstructor</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        constructor1<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//爆破，使用反射可以访问private的构造器/方法/属性</span>
        <span class="token class-name">Object</span> o2 <span class="token operator">=</span> constructor1<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token string">"形参"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>o2<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="通过反射访问类的属性"><a href="#通过反射访问类的属性" class="headerlink" title="通过反射访问类的属性"></a>通过反射访问类的属性</h4><p>  根据属性名获取Field对象  Field f = class对象.getDeclaredField(属性名)</p>
<p>  爆破：field.setAccessible(true);</p>
<p>  操作属性：</p>
<ul>
<li>field.set(o,值);</li>
<li>field.get(o);       //表示对象</li>
</ul>
<p>  注意：如果是静态属性，则set和get中的参数o可以为：null。</p>
<p>  在方法中如果有返回值，一律使用object类型接收。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">,</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">IllegalAccessException</span><span class="token punctuation">,</span> <span class="token class-name">InstantiationException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchMethodException</span><span class="token punctuation">,</span> <span class="token class-name">InvocationTargetException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchFieldException</span> <span class="token punctuation">&#123;</span>
       <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"com.qn.xc.User"</span><span class="token punctuation">;</span>
       <span class="token comment">//1.获取到指定类的Class对象</span>
       <span class="token class-name">Class</span><span class="token operator">&lt;</span><span class="token operator">?</span>  uclass <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">;</span>
 	    <span class="token class-name">Object</span> o <span class="token operator">=</span> uclass<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  		<span class="token comment">//2.获取指定类的指定属性</span>
       <span class="token class-name">Field</span> fieldName <span class="token operator">=</span> uclass<span class="token punctuation">.</span><span class="token function">getDeclaredField</span><span class="token punctuation">(</span><span class="token string">"fieldName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  		<span class="token comment">//3.如果是私有属性可以使用爆破</span>
    fieldName<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  		<span class="token comment">//4.获得指定对象的指定属性</span>
       <span class="token class-name">Object</span> o1 <span class="token operator">=</span> fieldName<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//静态属性.get(null)</span>
  		<span class="token comment">//5.将指定对象的指定属性设置为指定值</span>
       fieldName<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//静态属性.set(null,22)</span>
       <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>o1<span class="token punctuation">)</span><span class="token punctuation">;</span> 

   <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="通过反射访问类的方法"><a href="#通过反射访问类的方法" class="headerlink" title="通过反射访问类的方法"></a>通过反射访问类的方法</h4><p>  根据方法名和参数列表获取Method方法对象：</p>
<ul>
<li>Method m = class.getDeclaredMethod(方法名，XX.class);</li>
</ul>
<p>  获取对象： Object o = class.newInstance();</p>
<p>  爆破：m.setAccessible(true)</p>
<p>  访问: Object returnValue = m.invoke(o,实参列表)；</p>
<p>  如果是静态方法，则invoke的参数o可以写成null。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">,</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">IllegalAccessException</span><span class="token punctuation">,</span> <span class="token class-name">InstantiationException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchMethodException</span><span class="token punctuation">,</span> <span class="token class-name">InvocationTargetException</span><span class="token punctuation">,</span> <span class="token class-name">NoSuchFieldException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"com.qn.xc.User"</span><span class="token punctuation">;</span>
        <span class="token comment">//1.获取到指定类的Class对象</span>
        <span class="token class-name">Class</span><span class="token operator">&lt;</span><span class="token operator">?</span>  uclass <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Object</span> o <span class="token operator">=</span> uclass<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//2.调用普通的public方法。</span>
        <span class="token class-name">Method</span> dance1 <span class="token operator">=</span> uclass<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span><span class="token string">"dance1"</span><span class="token punctuation">,</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dance1<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span><span class="token string">"实参"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//3.调用私有方法</span>
        <span class="token class-name">Method</span> dance2 <span class="token operator">=</span> uclass<span class="token punctuation">.</span><span class="token function">getDeclaredMethod</span><span class="token punctuation">(</span><span class="token string">"dance2"</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dance2<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dance2<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span><span class="token string">"实参"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//4.调用静态方法</span>
        <span class="token class-name">Method</span> dance3 <span class="token operator">=</span> uclass<span class="token punctuation">.</span><span class="token function">getDeclaredMethod</span><span class="token punctuation">(</span><span class="token string">"dance3"</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dance3<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dance3<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span><span class="token string">"形参"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<h3 id="52-JDBC和连接池"><a href="#52-JDBC和连接池" class="headerlink" title="52. JDBC和连接池"></a>52. JDBC和连接池</h3><blockquote>
<p>JDBC为访问不同的数据库提供的同一的接口，为了使使用者屏蔽细节问题。  使用JDBC可以连接任何提供了JDBC驱动程序的数据库系统，从而完成对数据库的操作。</p>
</blockquote>
<ul>
<li>JDBC使java提供的一套用于操作数据库接口API，java程序员只需要面向这套接口编程即可，不同的数据库厂商，需要针对这套接口提供不同实现。</li>
</ul>
<h4 id="JDBC程序编写步骤"><a href="#JDBC程序编写步骤" class="headerlink" title="JDBC程序编写步骤"></a>JDBC程序编写步骤</h4><ol>
<li>前置工作：<ul>
<li>将mysql.jar 拷贝到该目录下，点击add to project  加入到项目中</li>
</ul>
</li>
<li>注册驱动  – 加载Driver类<ul>
<li>Driver driver = new Driver();</li>
</ul>
</li>
<li>获取连接  – 得到Connection<ul>
<li>String url = “jdbc:mysql://localhost:3306/db01”;    设置连接了协议，连接地址，连接数据库</li>
<li>Properties properties = new Properties();</li>
<li>properties.setProperty(“user”,”root”);   设置用户名</li>
<li>properties.setProperty(“password”,”fsc”);  设置密码</li>
<li>Connection connect = driver.connect(url, properties);  //建立连接</li>
</ul>
</li>
<li>执行增删改查  – 发送sql给mysql执行<ul>
<li>String sql = “sql语句”;   //设置要执行的sql语句</li>
<li>Statement statement = connect.createStatement();  //新建执行sql语句的statement对象</li>
<li>int i = statement.executeUpdate(sql); //执行sql语句得到返回值。如果返回值大于0则是影响的行数</li>
</ul>
</li>
<li>释放资源  – 关闭相关连接<ul>
<li>statement.close();   //释放操作对象连接</li>
<li>connect.close();  //释放数据库连接</li>
</ul>
</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//前置工作</span>
        <span class="token comment">//将mysql.jar 拷贝到该目录下，点解add to project  加入到项目中</span>

        <span class="token comment">//1.注册驱动</span>
        <span class="token class-name">Driver</span> driver <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Driver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//2.得到连接</span>
        <span class="token comment">/*
        *   1. jdbc:mysql:// 规定好的协议，通过jdbc的方式连接mysql
        *   2. localhost 主机，数据库所在的IP地址
        *   3. 3306  数据库所占用的端口
        *   4. db01  数据库名字
        *   5. musql的连接本质就是socket连接
        * */</span>
        <span class="token class-name">String</span> url <span class="token operator">=</span> <span class="token string">"jdbc:mysql://localhost:3306/db01"</span><span class="token punctuation">;</span>
        <span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//将用户名和密码放入properties中</span>
        properties<span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">,</span><span class="token string">"root"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        properties<span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"password"</span><span class="token punctuation">,</span><span class="token string">"fsc"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//连接指定数据库</span>
        <span class="token class-name">Connection</span> connect <span class="token operator">=</span> driver<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> properties<span class="token punctuation">)</span><span class="token punctuation">;</span>


        <span class="token comment">//3.执行增删改查</span>
        <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">"insert into lzy values(4,'豆包',590,'rz')"</span><span class="token punctuation">;</span>

        <span class="token class-name">Statement</span> statement <span class="token operator">=</span> connect<span class="token punctuation">.</span><span class="token function">createStatement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> i <span class="token operator">=</span> statement<span class="token punctuation">.</span><span class="token function">executeUpdate</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//返回的是操作影响的行数</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i  <span class="token number">0</span><span class="token operator">?</span><span class="token string">"成功"</span><span class="token operator">:</span><span class="token string">"失败"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//4.释放资源</span>
        statement<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connect<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="连接数据库："><a href="#连接数据库：" class="headerlink" title="连接数据库："></a>连接数据库：</h4><p>  使用DriverManager获得与数据库的连接对象</p>
<ul>
<li>getConnection(url,name,password);  //输入数据库的url，用户名，密码，获得连接对象。</li>
</ul>
<p>  使用Connection对象获得对应数据库的执行对象</p>
<ul>
<li>setAutoCommit(false)  //关闭事务的自动提交</li>
<li>commit()  //手动提交事务</li>
<li>rollback()  //手动回滚事务</li>
</ul>
<ul>
<li>createrStatement()  //创建Statement对象</li>
<li>preparedStement(sql)    //创建预处理对象</li>
</ul>
<h5 id="方式一"><a href="#方式一" class="headerlink" title="方式一"></a>方式一</h5><p>  使用Driver对象连接到数据库</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">connect01</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">Driver</span> driver <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Driver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//创建driver对象</span>
        <span class="token class-name">String</span> url <span class="token operator">=</span> <span class="token string">"jdbc:mysql://localhost:3306/db01"</span><span class="token punctuation">;</span>
        <span class="token comment">//将 用户名和密码放入到Properties 对象</span>
        <span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//说明 user 和 password 是规定好，后面的值根据实际情况写</span>
        properties<span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">,</span> <span class="token string">"root"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 用户</span>
        properties<span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"password"</span><span class="token punctuation">,</span> <span class="token string">"fsc"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//密码</span>
        <span class="token class-name">Connection</span> connect <span class="token operator">=</span> driver<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> properties<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>connect<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="方式二："><a href="#方式二：" class="headerlink" title="方式二："></a>方式二：</h5><p>  使用反射机制动态加载Driver类，使用反射加载Driver类 , 动态加载，更加的灵活，减少依赖性。</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">connect02</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">IllegalAccessException</span><span class="token punctuation">,</span> <span class="token class-name">InstantiationException</span><span class="token punctuation">,</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//使用反射加载Driver类 , 动态加载，更加的灵活，减少依赖性</span>
        <span class="token class-name">Class</span><span class="token operator">&lt;</span><span class="token operator">?</span>  aClass <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Driver</span> driver <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Driver</span><span class="token punctuation">)</span>aClass<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        <span class="token class-name">String</span> url <span class="token operator">=</span> <span class="token string">"jdbc:mysql://localhost:3306/db01"</span><span class="token punctuation">;</span>
        <span class="token comment">//将 用户名和密码放入到Properties 对象</span>
        <span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//说明 user 和 password 是规定好，后面的值根据实际情况写</span>
        properties<span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">,</span> <span class="token string">"root"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 用户</span>
        properties<span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"password"</span><span class="token punctuation">,</span> <span class="token string">"fsc"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//密码</span>
 
        <span class="token class-name">Connection</span> connect <span class="token operator">=</span> driver<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> properties<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"方式2="</span> <span class="token operator">+</span> connect<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="方式三"><a href="#方式三" class="headerlink" title="方式三"></a>方式三</h5><p>  使用DriverManager 替代 driver 进行统一管理</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">connect03</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IllegalAccessException</span><span class="token punctuation">,</span> <span class="token class-name">InstantiationException</span><span class="token punctuation">,</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
 
        <span class="token comment">//使用反射加载Driver</span>
        <span class="token class-name">Class</span><span class="token operator">&lt;</span><span class="token operator">?</span>  aClass <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Driver</span> driver <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Driver</span><span class="token punctuation">)</span> aClass<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        <span class="token comment">//创建url 和 user 和 password</span>
        <span class="token class-name">String</span> url <span class="token operator">=</span> <span class="token string">"jdbc:mysql://localhost:3306/db01"</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> user <span class="token operator">=</span> <span class="token string">"root"</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> password <span class="token operator">=</span> <span class="token string">"fsc"</span><span class="token punctuation">;</span>
 
        <span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">registerDriver</span><span class="token punctuation">(</span>driver<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//注册Driver驱动</span>
 
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> user<span class="token punctuation">,</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"第三种方式="</span> <span class="token operator">+</span> connection<span class="token punctuation">)</span><span class="token punctuation">;</span>
  		connection，<span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="方式四"><a href="#方式四" class="headerlink" title="方式四"></a>方式四</h5><p>  使用Class.forName 自动完成注册驱动，简化代码</p>
<p>  ==当动态加载Driver类时，会执行静态代码块==：</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">static</span> <span class="token punctuation">&#123;</span>
                <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
                    <span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">registerDriver</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Driver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> var1<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token string">"Can't register driver!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">connect04</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
       <span class="token comment">//使用反射加载了 Driver类</span>
       <span class="token comment">//在加载 Driver类时，完成注册</span>
       <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
       <span class="token comment">//创建url 和 user 和 password</span>
       <span class="token class-name">String</span> url <span class="token operator">=</span> <span class="token string">"jdbc:mysql://localhost:3306/db01"</span><span class="token punctuation">;</span>
       <span class="token class-name">String</span> user <span class="token operator">=</span> <span class="token string">"root"</span><span class="token punctuation">;</span>
       <span class="token class-name">String</span> password <span class="token operator">=</span> <span class="token string">"fsc"</span><span class="token punctuation">;</span>
       <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> user<span class="token punctuation">,</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span>
 
       <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"第4种方式~ "</span> <span class="token operator">+</span> connection<span class="token punctuation">)</span><span class="token punctuation">;</span>
 
   <span class="token punctuation">&#125;</span>
 <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>  mysql驱动在5.1.6之后无需 Class.forName(“com.mysql.jdbc.Driver”);从jdk1.5</p>
<p>  之后使用了jdbc4</p>
<p>  ，不需要显示调用 Class.forName()注册驱动，而是自动调用驱动jar包下META-INF\services\java.sql.Driver 文本中的类名称去注册。</p>
<h5 id="方式五"><a href="#方式五" class="headerlink" title="方式五"></a>方式五</h5><p>  在方式4的基础上改进，增加配置文件，让连接mysql更加灵活</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">connect05</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">,</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
 
        <span class="token comment">//通过Properties对象获取配置文件的信息</span>
        <span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        properties<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\mysql.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//获取相关的值</span>
        <span class="token class-name">String</span> user <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> password <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"password"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> driver <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> url <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"url"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>driver<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//建议写上</span>
 
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> user<span class="token punctuation">,</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span>
 
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"方式5 "</span> <span class="token operator">+</span> connection<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="ResultSet-结果集"><a href="#ResultSet-结果集" class="headerlink" title="ResultSet 结果集"></a>ResultSet 结果集</h4><p>  表示数据库结果集的数据表，通常通过执行数据看查询语句生成。</p>
<p>  ResultSet对象保持一个光标指向当前的数据行，==最初光标位于第一行之前==，next方法将光标移动到下一行，并且由于在ResultSet对象中没有更多行时返回false，因此可以在while循环中使用循环遍历结果集。</p>
<p>  默认ResultSet对象不可以更新，并且只有一个向前移动的光标，只能从第一行到最后一行迭代一次，可以生成更新不敏感的结果集。</p>
<ul>
<li>ResultSet对象.next()  //使光标向下移动一行,如果没有下一行返回false</li>
<li>ResultSet对象.previous()  //使光标向上移动一行,如果没有下一行返回false</li>
<li>ResultSet对象.getXxx(索引 || 列名)     //获得当前行指定索引或者列名的值</li>
<li>ResultSet对象.getObject(索引 || 列名) </li>
</ul>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span><span class="token punctuation">,</span> <span class="token class-name">ClassNotFoundException</span> <span class="token punctuation">&#123;</span>
     <span class="token class-name">String</span> url <span class="token operator">=</span> <span class="token string">"jdbc:mysql://localhost:3306/db01"</span><span class="token punctuation">;</span>
     <span class="token class-name">String</span> user <span class="token operator">=</span> <span class="token string">"root"</span><span class="token punctuation">;</span>
     <span class="token class-name">String</span> password <span class="token operator">=</span> <span class="token string">"fsc"</span><span class="token punctuation">;</span>

    <span class="token comment">//动态加载driver类，执行静态代码块获得driverManage类</span>
     <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

     <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> user<span class="token punctuation">,</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span>

     <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>connection<span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token class-name">Statement</span> statement <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">createStatement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

     <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">"select * from lzy"</span><span class="token punctuation">;</span>


     <span class="token comment">//ResultSet 是接口</span>
     <span class="token class-name">ResultSet</span> resultSet <span class="token operator">=</span> statement<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>resultSet<span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token keyword">while</span><span class="token punctuation">(</span>resultSet<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token comment">//resultSet.next()如果下一行没有数据则返回false</span>
         <span class="token keyword">int</span> id <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getInt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token class-name">String</span> name <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">int</span> tal <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getInt</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token class-name">String</span> address <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>id <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> tal <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> address<span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token punctuation">&#125;</span>
     resultSet<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     statement<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

 <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="Statement"><a href="#Statement" class="headerlink" title="Statement"></a>Statement</h4><ol>
<li><p>Statement对象，用于执行静态sql语句并且返回生成的结果对象。</p>
</li>
<li><p>在连接建立后，需要对数据库进行访问，执行命名或者是sql语句，可以通过：</p>
<ul>
<li>Statement [存在sql注入]</li>
<li>PreparedStatement [预处理]</li>
<li>CallableStatement [存储过程]</li>
</ul>
</li>
<li><p>Statement对象执行sql语句存在sql注入的风险。</p>
</li>
<li><p>sql注入是利用某些系统没有对用户输入的数据进行充分的检查，而是用户输入数据种植呼入非法的sql语句段或者命令，恶意攻击数据库。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">//输入的用户名： 1' or</span>
 <span class="token comment">//输入密码： or '1'='1</span>
 <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> <span class="token keyword">user</span>
 <span class="token keyword">where</span> name <span class="token operator">=</span> <span class="token string">'用户名'</span> <span class="token operator">and</span> pwd <span class="token operator">=</span> <span class="token string">'密码'</span>
 <span class="token comment">//sql注入后</span>
 <span class="token string">'1'</span> <span class="token operator">or</span>  <span class="token string">' and pwd = '</span><span class="token operator">or</span> <span class="token string">'1'</span><span class="token operator">=</span><span class="token string">'1'</span>  <span class="token comment">//肯定成立</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
<li><p>要防止sql主语只需要使用PreparedStatement(从Startement扩展而来)取代Statement就要可以。</p>
<ul>
<li>executeUpdate(sql)  //执行修改，添加，删除语句，返回影响的行数</li>
<li>executeQuery(sql)  //执行查询语句，返回影响的行数。</li>
<li>execute(sql)  //执行任意sql，返回布尔值</li>
</ul>
</li>
</ol>
<h4 id="PreparedStatement"><a href="#PreparedStatement" class="headerlink" title="PreparedStatement"></a>PreparedStatement</h4><p>  PreparedStatement执行的sql语句中的参数使用问号(?)来表示，调用PreparedStatement对象的setXXX()方法来设置这些参数。 setXXX()方法有两个参数，第一个参数要设置sql语句中的参数的索引(从1开始)，第二个是设置sql语句中参数的值。</p>
  <pre class="line-numbers language-sql" data-language="sql"><code class="language-sql">String <span class="token keyword">sql</span> <span class="token operator">=</span> <span class="token string">"select count(*) from 表名 where username=？ and  password =？"</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>  调用executeQuery()，返回ResultSet结果集对象(查询)。</p>
<p>  调用executeUpdate(),执行更新，包括增加，删除，修改，返回一个int类型，如果大于0返回影响的行数。</p>
<p>  预处理：</p>
<ul>
<li>不再使用 + 拼接sql语句，减少语法错误。</li>
<li>有效的解决了sql注入问题。</li>
<li>大大减少了便于次数，效率提高。</li>
</ul>
<p>  主要方法：</p>
<ul>
<li>executeUpdate(sql)  //执行修改，添加，删除语句，返回影响的行数</li>
<li>executeQuery(sql)  //执行查询语句，返回影响的行数。</li>
<li>execute(sql)  //执行任意sql，返回布尔值</li>
<li>setXxx(占位符索引,占位符的值)   //设置对应占位符的值，解决sql注入</li>
<li>setObject(占位符索引,占位符的值)</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ClassNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> url <span class="token operator">=</span> <span class="token string">"jdbc:mysql://localhost:3306/db01"</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> user <span class="token operator">=</span> <span class="token string">"root"</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> password <span class="token operator">=</span> <span class="token string">"fsc"</span><span class="token punctuation">;</span>

        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span>user<span class="token punctuation">,</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//sql语句的？相当于占位符</span>
        <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">"select * from lzy where name=? and tal=?"</span><span class="token punctuation">;</span>

        <span class="token comment">//preparedStatement是实现了PreparedStatement接口的实现类的对象</span>
        <span class="token class-name">PreparedStatement</span> preparedStatement <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">prepareStatement</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>
        preparedStatement<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">"lzy"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        preparedStatement<span class="token punctuation">.</span><span class="token function">setInt</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">12121</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">ResultSet</span> resultSet <span class="token operator">=</span> preparedStatement<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>resultSet<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token keyword">int</span> id <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getInt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">String</span> name <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> tal <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getInt</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">String</span> address <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>id <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> tal <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> address<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

        resultSet<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        preparedStatement<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="jdbcAPI总结"><a href="#jdbcAPI总结" class="headerlink" title="jdbcAPI总结"></a>jdbcAPI总结</h4><h4 id="JDBCUtile"><a href="#JDBCUtile" class="headerlink" title="JDBCUtile"></a>JDBCUtile</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>doubao<span class="token punctuation">.</span>jdbcUtils</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">FileInputStream</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Properties</span></span><span class="token punctuation">;</span>

<span class="token comment">/**
 * 这是一个工具类，完成mysql数据库的的链接和关闭资源
 */</span>


<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JDBCUtils</span> <span class="token punctuation">&#123;</span>
    <span class="token comment">//连接数据库的使用</span>

    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">String</span> user<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">String</span> password<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">String</span> url<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">String</span> driver<span class="token punctuation">;</span>

    <span class="token keyword">static</span> <span class="token punctuation">&#123;</span>

        <span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
            properties<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\musqljdbc.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            url <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"url"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            password <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"password"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            user <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            driver <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>driver<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> <span class="token operator">|</span> <span class="token class-name">ClassNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//将编译异常转换为运行异常。</span>
            <span class="token comment">//调用者可以选择捕获异常也可以选择默认处理异常</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token comment">//获得数据库连接</span>
        <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Connection</span> <span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

        <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">return</span> <span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span>user<span class="token punctuation">,</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//将编译异常转换为运行异常</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token comment">/**
     *
     * @param set  结果集
     * @param statement statement或者statementMarager
     * @param connection 连接对象
     * 如果需要关闭资源就传入响应对象，否则传入null
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">close</span><span class="token punctuation">(</span><span class="token class-name">ResultSet</span> set<span class="token punctuation">,</span> <span class="token class-name">Statement</span> statement<span class="token punctuation">,</span><span class="token class-name">Connection</span> connection<span class="token punctuation">)</span>  <span class="token punctuation">&#123;</span>

        <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>set <span class="token operator">!=</span><span class="token keyword">null</span> <span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                set<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>statement <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                statement<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>connection <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

    <span class="token punctuation">&#125;</span>


<span class="token punctuation">&#125;</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>properties文件<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
url<span class="token operator">=</span>jdbc<span class="token operator">:</span>mysql<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token operator">:</span><span class="token number">3306</span><span class="token operator">/</span>db01
name<span class="token operator">=</span>root
password<span class="token operator">=</span>fsc
driver<span class="token operator">=</span><span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>mysql<span class="token punctuation">.</span>jdbc<span class="token punctuation">.</span></span>Driver</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="事务"><a href="#事务" class="headerlink" title="事务"></a>事务</h4><p>  基本介绍</p>
<ol>
<li>JDBC程序中当一个Connection对象创建时，==默认情况下是自动提交事务==，每次执行一个SQL语句时，如果执行成功，就会向数据库自动提交，而不能回滚。</li>
<li>JDBC程序为了让多个sql语句作为一个整体执行，需要使用事务。</li>
<li>调用Connection的setAutoCommit(false)可以取消自动提交事务。</li>
<li>在所有的sql语句都成功执行后，调用commit();方法提交事务。</li>
<li>在其中某个操作失败或出现异常时，调用rollback()方法回滚事务。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token class-name">PreparedStatement</span> preparedStatement <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token class-name">ResultSet</span> resultSet <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
            connection <span class="token operator">=</span> <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//去除连接的自动提交事务</span>
            connection<span class="token punctuation">.</span><span class="token function">setAutoCommit</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">"select * from lzy"</span><span class="token punctuation">;</span>
            preparedStatement <span class="token operator">=</span>connection<span class="token punctuation">.</span><span class="token function">prepareStatement</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>
            resultSet <span class="token operator">=</span> preparedStatement<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">while</span><span class="token punctuation">(</span>resultSet<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token keyword">int</span> id <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getInt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">String</span> name <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">int</span> tal <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getInt</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">String</span> like <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>id <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> tal <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> like<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
            <span class="token comment">//如果没有出现异常手动提交事务</span>
            connection<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> throwables<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//如果出现异常回滚事务</span>
            connection<span class="token punctuation">.</span><span class="token function">rollback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span>resultSet<span class="token punctuation">,</span>preparedStatement<span class="token punctuation">,</span>connection<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="批处理"><a href="#批处理" class="headerlink" title="批处理"></a>批处理</h4><ol>
<li>当需要成批插入或者更新记录时，可以采用java的批量更新机制，这一机制允许多条语句一次性提交给数据库进行批量处理，通常情况下，批量处理比单独提交处理更有效率。(批量处理可以减少发送sql语句的网络开销)</li>
<li>JDBC的批处理语句包括下面的方法：<ul>
<li>addBatch():  添加需要处理的批处理的sql语句或者参数。</li>
<li>executeBatch():执行需要批量处理的sql语句或者参数。</li>
<li>clearBatch(): 清空批处理包的语句。</li>
</ul>
</li>
<li>==JDBC连接mysql时，如果要使用批处理功能，在url中添加批处理功能参数   <code>?rewriteBatchedStatements=true</code>==</li>
<li>批处理往往和PreparedStatement一起搭配使用，可以减少编译次数，减少运行次数，运行效率大大提升。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

        <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">"insert into admin1 values(?,?)"</span><span class="token punctuation">;</span>

            <span class="token class-name">PreparedStatement</span> prepareStatement <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">prepareStatement</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>
            
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"开始执行"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number">5000</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                prepareStatement<span class="token punctuation">.</span><span class="token function">setInt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
                prepareStatement<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">"lzy"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                prepareStatement<span class="token punctuation">.</span><span class="token function">addBatch</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">//如果加入例如1000跳数据就开始执行</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">%</span><span class="token number">1000</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                    prepareStatement<span class="token punctuation">.</span><span class="token function">executeBatch</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    prepareStatement<span class="token punctuation">.</span><span class="token function">clearBatch</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
            

            <span class="token class-name">JDBCUtils</span><span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span>prepareStatement<span class="token punctuation">,</span>connection<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

    <span class="token punctuation">&#125;</span>

<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>
url<span class="token operator">=</span>jdbc<span class="token operator">:</span>mysql<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token operator">:</span><span class="token number">3306</span><span class="token operator">/</span>db01<span class="token operator">?</span>rewriteBatchedStatements<span class="token operator">=</span><span class="token boolean">true</span>
name<span class="token operator">=</span>root
password<span class="token operator">=</span>fsc
driver<span class="token operator">=</span><span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>mysql<span class="token punctuation">.</span>jdbc<span class="token punctuation">.</span></span>Driver</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="数据库连接池"><a href="#数据库连接池" class="headerlink" title="数据库连接池"></a>数据库连接池</h4><ol>
<li>传统的JDBC数据库了使用DriverManager来获取，每次向数据库建立连接的时候都将Connextion加载到内存中，再验证ip地址，用户名和密码，需要数据库连接的时候，就向数据库请求一个，频繁的进行数据库连接操作将占用很多的系统资源，容易造成服务器崩溃。</li>
<li>每一次数据库连接，使用完成后都得断开，如果程序出现异常而未能关闭，将导致数据库内存泄露，最终将导致重启数据库。</li>
<li>传统获取连接的方式，不能控制创建的来连接数量，如果连接过多，也可能倒是内存泄露，mysql崩溃。</li>
<li>解决传统开发中的数据库来连接问题，可以采用数据库连接池技术(connextion pool)</li>
</ol>
<p>  数据库连接池机制：</p>
<ul>
<li>预先在缓冲池中放入一定数量的来连接，当需要建立数据库连接时，只需要从”缓冲池“中取出来一个，使用完毕之后再放回去。</li>
<li>数据库连接池负责分配，管理和释放数据库来凝结，它允许应该程序重复使用一个现有的数据库连接，而不是重新建立一个。</li>
<li>当应用程序向连接池请求的连接数超过最大的连接数量时，和这些请求被加到等待队列中。<ul>
<li>从连接池取出连接</li>
<li>使用连接操作mysql数据库</li>
<li>连接放回连接池(该连接可以重复使用)</li>
</ul>
</li>
<li>在数据库连接池技术中close方法不是断掉连接，只是将连接放回连接池</li>
</ul>
<p>数据库连接池种类</p>
<ol>
<li>jdbc的数据库连接池使用javax.sql.DataSource来表示，DataSource只是一个接口，该接口通常由第三方提供。</li>
<li>C3P0数据库连接池，速度相对较慢，稳定性好。</li>
<li>DBCP数据库连接池，速度相对C3P0较快，但是不稳定。</li>
<li>Proxool数据库连接池，有监控连接池状态的功能，稳定性较C3P0差一些。</li>
<li>BoneCP 数据库连接池，速度快。</li>
<li>Druid(德鲁伊)是阿里提供的数据库连接池集成C3P0，DBCP，Proxool优点于一身的数据库连接池。</li>
</ol>
<h5 id="c3p0"><a href="#c3p0" class="headerlink" title="c3p0"></a>c3p0</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//需要引进jar包</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">,</span> <span class="token class-name">PropertyVetoException</span><span class="token punctuation">,</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//1. 创建一个数据源对象</span>
        <span class="token class-name">ComboPooledDataSource</span> comboPooledDataSource <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComboPooledDataSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//2. 通过配置文件mysql.properties 获取相关连接的信息</span>
        <span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        properties<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\musqljdbc.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//读取相关的属性值</span>
        <span class="token class-name">String</span> user <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getPoperty</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> password <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"password"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> url <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"url"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> driver <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//给数据源 comboPooledDataSource 设置相关的参数</span>
        <span class="token comment">//注意：连接管理是由 comboPooledDataSource 来管理</span>
        comboPooledDataSource<span class="token punctuation">.</span><span class="token function">setDriverClass</span><span class="token punctuation">(</span>driver<span class="token punctuation">)</span><span class="token punctuation">;</span>
        comboPooledDataSource<span class="token punctuation">.</span><span class="token function">setJdbcUrl</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>
        comboPooledDataSource<span class="token punctuation">.</span><span class="token function">setUser</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
        comboPooledDataSource<span class="token punctuation">.</span><span class="token function">setPassword</span><span class="token punctuation">(</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//设置初始化连接数</span>
        comboPooledDataSource<span class="token punctuation">.</span><span class="token function">setInitialPoolSize</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//最大连接数</span>
        comboPooledDataSource<span class="token punctuation">.</span><span class="token function">setMaxPoolSize</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//测试连接池的效率, 测试对mysql 5000次操作</span>
        <span class="token keyword">long</span> start <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">5000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">Connection</span> connection <span class="token operator">=</span> comboPooledDataSource<span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//这个方法就是从 DataSource 接口实现的</span>
            <span class="token comment">//System.out.println("连接OK");</span>
            connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token keyword">long</span> end <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//c3p0 5000连接mysql 耗时=391</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"c3p0 5000连接mysql 耗时="</span> <span class="token operator">+</span> <span class="token punctuation">(</span>end <span class="token operator">-</span> start<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//将c3p0提供的c3p0.config.xml</span>
    <span class="token comment">//该文件指定了连接数据库和连接池的相关参数</span>
        <span class="token comment">//方式1： 相关参数，在程序中指定user, url , password等</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ComboPooledDataSource</span> comboPooledDataSource <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComboPooledDataSource</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> comboPooledDataSource<span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>connection<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">&#125;</span>

<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>c3p0<span class="token punctuation">.</span>config<span class="token punctuation">.</span>xml
<span class="token operator">&lt;</span>c3p0<span class="token operator">-</span>config 
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>   数据源的名称<span class="token operator">--</span> 
  <span class="token operator">&lt;</span>named<span class="token operator">-</span>config name<span class="token operator">=</span><span class="token string">"hello"</span>  
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 驱动类 <span class="token operator">--</span> 
  <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"driverClass"</span> <span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>mysql<span class="token punctuation">.</span>jdbc<span class="token punctuation">.</span></span>Driver</span><span class="token operator">&lt;</span><span class="token operator">/</span>property 
  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> url<span class="token operator">--</span> 
  	<span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"jdbcUrl"</span> jdbc<span class="token operator">:</span>mysql<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token operator">:</span><span class="token number">3306</span><span class="token operator">/</span>db01<span class="token operator">&lt;</span><span class="token operator">/</span>property 
  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 用户名 <span class="token operator">--</span> 
  		<span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"user"</span> root<span class="token operator">&lt;</span><span class="token operator">/</span>property 
  		<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 密码 <span class="token operator">--</span> 
  	<span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"password"</span> fsc<span class="token operator">&lt;</span><span class="token operator">/</span>property 
  	<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 每次增长的连接数<span class="token operator">--</span> 
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"acquireIncrement"</span> <span class="token number">5</span><span class="token operator">&lt;</span><span class="token operator">/</span>property 
    <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 初始的连接数 <span class="token operator">--</span> 
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"initialPoolSize"</span> <span class="token number">10</span><span class="token operator">&lt;</span><span class="token operator">/</span>property 
    <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 最小连接数 <span class="token operator">--</span> 
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"minPoolSize"</span> <span class="token number">5</span><span class="token operator">&lt;</span><span class="token operator">/</span>property 
   <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 最大连接数 <span class="token operator">--</span> 
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"maxPoolSize"</span> <span class="token number">10</span><span class="token operator">&lt;</span><span class="token operator">/</span>property 

	<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 可连接的最多的命令对象数 <span class="token operator">--</span> 
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"maxStatements"</span> <span class="token number">5</span><span class="token operator">&lt;</span><span class="token operator">/</span>property  
    
    <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 每个连接对象可连接的最多的命令对象数 <span class="token operator">--</span> 
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"maxStatementsPerConnection"</span> <span class="token number">2</span><span class="token operator">&lt;</span><span class="token operator">/</span>property 
  <span class="token operator">&lt;</span><span class="token operator">/</span>named<span class="token operator">-</span>config 
<span class="token operator">&lt;</span><span class="token operator">/</span>c3p0<span class="token operator">-</span>config <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="Druid-德鲁伊"><a href="#Druid-德鲁伊" class="headerlink" title="Druid(德鲁伊)"></a>Druid(德鲁伊)</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//加入druid的jar包，加入项目</span>
        <span class="token comment">//加入配置文件</span>
        <span class="token comment">//创建Propertise对象</span>
        <span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        properties<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\druid.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//创建一个指定参数数据源</span>
        <span class="token class-name">DataSource</span> dataSource <span class="token operator">=</span> <span class="token class-name">DruidDataSourceFactory</span><span class="token punctuation">.</span><span class="token function">createDataSource</span><span class="token punctuation">(</span>properties<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//获取数据源连接</span>
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> dataSource<span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>connection<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>druid<span class="token punctuation">.</span>properties
#key<span class="token operator">=</span>value
driverClassName<span class="token operator">=</span><span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>mysql<span class="token punctuation">.</span>jdbc<span class="token punctuation">.</span></span>Driver</span>
url<span class="token operator">=</span>jdbc<span class="token operator">:</span>mysql<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token operator">:</span><span class="token number">3306</span><span class="token operator">/</span>db01<span class="token operator">?</span>rewriteBatchedStatements<span class="token operator">=</span><span class="token boolean">true</span>
#url<span class="token operator">=</span>jdbc<span class="token operator">:</span>mysql<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token operator">:</span><span class="token number">3306</span><span class="token operator">/</span>db01
username<span class="token operator">=</span>root
password<span class="token operator">=</span>fsc
#initial connection <span class="token class-name">Size</span>
initialSize<span class="token operator">=</span><span class="token number">10</span>
#min idle connecton size
minIdle<span class="token operator">=</span><span class="token number">5</span>
#max active connection size
maxActive<span class="token operator">=</span><span class="token number">20</span>
#max wait time <span class="token punctuation">(</span><span class="token number">5000</span> mil seconds<span class="token punctuation">)</span>
maxWait<span class="token operator">=</span><span class="token number">5000</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="JDBCUtilsByDruid"><a href="#JDBCUtilsByDruid" class="headerlink" title="JDBCUtilsByDruid"></a>JDBCUtilsByDruid</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>doubao<span class="token punctuation">.</span>jdbcUtils</span><span class="token punctuation">;</span>
<span class="token comment">//使用druid连接池技术</span>
<span class="token comment">//需要添加druid配置文件，引入druid的jar包</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>alibaba<span class="token punctuation">.</span>druid<span class="token punctuation">.</span>pool<span class="token punctuation">.</span></span><span class="token class-name">DruidDataSourceFactory</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>sql<span class="token punctuation">.</span></span><span class="token class-name">DataSource</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">FileInputStream</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span></span><span class="token class-name">Connection</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span></span><span class="token class-name">ResultSet</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span></span><span class="token class-name">SQLException</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span></span><span class="token class-name">Statement</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Properties</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JDBCUtilsByDruid</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">DataSource</span> ds<span class="token punctuation">;</span>

    <span class="token comment">//静态代码块</span>
    <span class="token keyword">static</span><span class="token punctuation">&#123;</span>

        <span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
            properties<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"src\\druid.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            ds <span class="token operator">=</span> <span class="token class-name">DruidDataSourceFactory</span><span class="token punctuation">.</span><span class="token function">createDataSource</span><span class="token punctuation">(</span>properties<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>


    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Connection</span> <span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> ds<span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>


    <span class="token comment">//在数据库连接池技术中close方法不是断掉连接，只是将连接放回连接池</span>
    <span class="token comment">/**
     *
     * @param set  结果集
     * @param statement statement或者statementMarager
     * @param connection 连接对象
     * 如果需要关闭资源就传入响应对象，否则传入null
     * 并不是关闭数据库连接而是将连接放回连接池
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">close</span><span class="token punctuation">(</span><span class="token class-name">ResultSet</span> set<span class="token punctuation">,</span> <span class="token class-name">Statement</span> statement<span class="token punctuation">,</span> <span class="token class-name">Connection</span> connection<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

        <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>set <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                set<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>statement <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                statement<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>connection <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>druid<span class="token punctuation">.</span>properties配置文件
  #key<span class="token operator">=</span>value
driverClassName<span class="token operator">=</span><span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>mysql<span class="token punctuation">.</span>jdbc<span class="token punctuation">.</span></span>Driver</span>
url<span class="token operator">=</span>jdbc<span class="token operator">:</span>mysql<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token operator">:</span><span class="token number">3306</span><span class="token operator">/</span>db01<span class="token operator">?</span>rewriteBatchedStatements<span class="token operator">=</span><span class="token boolean">true</span>
#url<span class="token operator">=</span>jdbc<span class="token operator">:</span>mysql<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token operator">:</span><span class="token number">3306</span><span class="token operator">/</span>db01
username<span class="token operator">=</span>root
password<span class="token operator">=</span>fsc
#initial connection <span class="token class-name">Size</span>
initialSize<span class="token operator">=</span><span class="token number">10</span>
#min idle connecton size
minIdle<span class="token operator">=</span><span class="token number">5</span>
#max active connection size
maxActive<span class="token operator">=</span><span class="token number">20</span>
#max wait time <span class="token punctuation">(</span><span class="token number">5000</span> mil seconds<span class="token punctuation">)</span>
maxWait<span class="token operator">=</span><span class="token number">5000</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="Apache-—-DBUtils"><a href="#Apache-—-DBUtils" class="headerlink" title="Apache — DBUtils"></a>Apache — DBUtils</h4><p>  基本介绍：</p>
<p>  commons-dbutils 是Apache组织提供的一个开源的JDBC工具类库，他是对JDBC的封装，使用dbutils能极大简化jdbc编码的工作量。</p>
<p>  DbUtils</p>
<ol>
<li>QueryRunner类：该类封装了SQL执行，是==线程安全的==，可以实现增，删，改，查，批处理等操作。</li>
<li>ResultSetHandler接口：该接口用于处理Java.sql.ResultSet,将数据按照要求转换为另一种形式。</li>
</ol>
<ol>
<li><p>得到指定数据库的连接</p>
<ul>
<li><code>Connection connection = JDBCUtilsByDruid.getConnection();</code></li>
</ul>
</li>
<li><p>引入DBUtils相关的jar包，加入到本项目中.</p>
</li>
<li><p>创建对应表的javaBean类。</p>
</li>
<li><p>创建QueryRunner 对象:</p>
<ul>
<li><code>QueryRunner queryRunner = new QueryRunner();</code></li>
</ul>
</li>
<li><p>执行相关的方法，返回ArrayList结果集</p>
<ul>
<li>QueryRunner对象.query(连接对象，sql语句，new BeanListHandler&lt; (javaBean对象.class)，sql语句中的占位符所代表的值)</li>
</ul>
<ul>
<li><p><code>String sql = &quot;select * from lzy&quot;;   </code>                                             </p>
</li>
<li><p><code> List&lt;lzyBean  list = queryRunner.query(connection, sql, new BeanListHandler&lt; (lzyBean.class));</code></p>
</li>
</ul>
</li>
<li><p>关闭连接</p>
<ul>
<li><code>JDBCUtilsByDruid.close(null,null,connection);</code></li>
</ul>
</li>
</ol>
<h5 id="查询多列对象"><a href="#查询多列对象" class="headerlink" title="查询多列对象"></a>查询多列对象</h5><p>   query(连接对象，sql，new BeanListHandler&lt; (javaBean对象.class)，1）</p>
<p>   new BeanListHandler&lt; (javaBean对象.class)查询多行多列的返回值，返回的是一个javaBean对象组成的ArrayList数组。</p>
<ol>
<li>query()得到resultset,将resultset中的数据添加到相应的javaBean中，再将javaBean对象放入ArrayList数组中。</li>
<li>new BeanListHandler&lt; (javaBean对象.class),底层使用反射机制，获取javaBean中的属性，将然后进行封装。</li>
<li>传入的sql语句中可以使用<code>？</code> 当作占位符，再query()方法最后传入相应的值，可以传递多个值。sql语句查询到的会传值到javaBean对象，没有查询赋值为null。</li>
<li>底层中得到的Resultset和PreparedStatement的对象会在query中关闭。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span> <span class="token punctuation">&#123;</span>

        <span class="token comment">//1.得到连接</span>
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">JDBCUtilsByDruid</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//2.引入DBUtils相关的jar包，加入到本项目中</span>

        <span class="token comment">//3.创建QueryRunner 对象</span>
        <span class="token class-name">QueryRunner</span> queryRunner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QueryRunner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//4. 执行相关的方法，返回ArrayList结果集</span>
        <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">"select * from lzy"</span><span class="token punctuation">;</span>

<span class="token comment">//        List&lt;lzyBean  list = queryRunner.query(连接对象, sql语句, new BeanListHandler&lt; (javaBean类.class),占位符对应的值);</span>

        <span class="token class-name">List</span><span class="token operator">&lt;</span>lzyBean  list <span class="token operator">=</span> queryRunner<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> sql<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">BeanListHandler</span><span class="token operator">&lt;</span> <span class="token punctuation">(</span>lzyBean<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">for</span><span class="token punctuation">(</span>lzyBean lzy <span class="token operator">:</span> list<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>lzy<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        <span class="token comment">//关闭连接</span>
        <span class="token class-name">JDBCUtilsByDruid</span><span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span><span class="token keyword">null</span><span class="token punctuation">,</span>connection<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="查询单行对象"><a href="#查询单行对象" class="headerlink" title="查询单行对象"></a>查询单行对象</h5><p>  query(连接对象，sql，new BeanHandler&lt; (javaBean对象.class)，1）</p>
<p>  使用new BeanHandler&lt; (javaBean对象.class)查询到单行返回值，只返回一个对应的javaBean对象。</p>
<p>  如果没有查询到返回null。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//获得数据库连接</span>
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">JDBCUtilsByDruid</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//创建QueryRunner对象</span>
        <span class="token class-name">QueryRunner</span> queryRunner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QueryRunner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">"select * from lzy where id=?"</span><span class="token punctuation">;</span>

        <span class="token comment">//执行单列查询，返回对应列的javaBean类</span>
        lzyBean lzyBean <span class="token operator">=</span> queryRunner<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> sql<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">BeanHandler</span><span class="token operator">&lt;</span> <span class="token punctuation">(</span>lzyBean<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>lzyBean<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">JDBCUtilsByDruid</span><span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span><span class="token keyword">null</span><span class="token punctuation">,</span>connection<span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="查询单行单列对象"><a href="#查询单行单列对象" class="headerlink" title="查询单行单列对象"></a>查询单行单列对象</h5><p>  query(连接对象，sql，new ScalarHandler()，1）</p>
<p>  new ScalarHandler()查询单行单列的一个返回值，返回的是一个object对象。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//获得数据库连接</span>
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">JDBCUtilsByDruid</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//创建QueryRunner对象</span>
        <span class="token class-name">QueryRunner</span> queryRunner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QueryRunner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">"select name from lzy where id=?"</span><span class="token punctuation">;</span>

        <span class="token comment">//执行单行单列查询，返回对应列的javaBean类</span>
        <span class="token comment">//返回的是一个值</span>
        <span class="token class-name">Object</span> query <span class="token operator">=</span> queryRunner<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> sql<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">ScalarHandler</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>query<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">JDBCUtilsByDruid</span><span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span><span class="token keyword">null</span><span class="token punctuation">,</span>connection<span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="dml操作"><a href="#dml操作" class="headerlink" title="dml操作"></a>dml操作</h5><p>  QueryRunner对象.update(connection,sql语句,sql语句中的占位符代表值……)用来执行dml操作，返回的是影响的行数。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//获得数据库连接</span>
        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token class-name">JDBCUtilsByDruid</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//创建QueryRunner对象</span>
        <span class="token class-name">QueryRunner</span> queryRunner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QueryRunner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">"update lzy set name =? where id = ?"</span><span class="token punctuation">;</span>

  		<span class="token comment">//执行修改操作，返回影响到的行数</span>
        <span class="token keyword">int</span> affectedRow <span class="token operator">=</span> queryRunner<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span>sql<span class="token punctuation">,</span><span class="token string">"name"</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>；

        <span class="token class-name">JDBCUtilsByDruid</span><span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> connection<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="DAO-数据访问对象-data-access-object"><a href="#DAO-数据访问对象-data-access-object" class="headerlink" title="DAO 数据访问对象(data access object)"></a>DAO 数据访问对象(data access object)</h4><p>  apache-dbutils + druid 简化了JDBC开发，但是还有不足：</p>
<ol>
<li>SQL语句是固定的，不能通过参数传入，通用性不好，需要进行改进，更方便执行增删改查。 </li>
<li>对于select操作，如果有返回值，返回类型不能固定，需要使用泛型。</li>
<li>将来的表很多业务需求复杂，不看可能只靠一个Java类来实现。</li>
</ol>
<p>  通用类被称为BasicDao，是专门和数据库交互的，即完成对数据库(表)的crud操作。</p>
<p>  在BasicDao的基础上，实现一张表对应一个Dao，更好的完成功能。</p>
<h4 id="53-正则表达式"><a href="#53-正则表达式" class="headerlink" title="53. 正则表达式"></a>53. 正则表达式</h4><p>  正则表达式(regular expression)是对字符串执行模式匹配的技术。</p>
<p>  正则表达式就是使用某种模式去匹配字符串的一个公式，正则表达式在需要编程语言中都被支持。</p>
<p>  分组：</p>
<ul>
<li>我们可以用圆括号组成一个比较复杂的匹配模式。那么一个圆括号的部分我们可以看作是一个子表达式/一个分组</li>
</ul>
<p>  捕获：</p>
<ul>
<li>把正则表达式中子表达式(分组匹配)的内容，保存到内存中以数字编号或者显示命名的组里，方便以后引用，从左到右，以分组的左括号为标志，第一个出现的组号为1，第二个为2，组号0代表整个正则表达式。</li>
</ul>
<p>  反向引用：</p>
<ul>
<li>圆括号的内容被捕获后，可以在这个==括号后被使用==，从而写出一个比较使用的匹配模式，这种引用即可以实在正则表达式内部，也可以是在正则表达式外部，内部反向引用\\分组号，外部反向引用$分组号</li>
</ul>
<h5 id="使用正则表达式"><a href="#使用正则表达式" class="headerlink" title="使用正则表达式"></a>使用正则表达式</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//得到字符串中符合正则表达式的子字符串</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

        <span class="token class-name">String</span> str<span class="token operator">=</span> <span class="token string">"这是被匹配的字符串"</span><span class="token punctuation">;</span>
		
  		<span class="token comment">//1.定义相应的正则表达式规则</span>
        <span class="token class-name">String</span> regStr <span class="token operator">=</span> <span class="token string">"\\d&#123;3&#125;"</span><span class="token punctuation">;</span>
        <span class="token comment">//2.创建一个pattern对象，指定正则表达式规则</span>
        <span class="token class-name">Pattern</span> pattern <span class="token operator">=</span> <span class="token class-name">Pattern</span><span class="token punctuation">.</span><span class="token function">compile</span><span class="token punctuation">(</span>regStr<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//3.创建一个匹配器对象</span>
        <span class="token comment">//匹配器就是按照模式对象到指定的字符串中匹配，找到相应的字符串</span>
        <span class="token class-name">Matcher</span> matcher <span class="token operator">=</span> pattern<span class="token punctuation">.</span><span class="token function">matcher</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//3.开始循环匹配，每次执行find函数找到下一个符合正则表达式的值。</span>
        <span class="token keyword">while</span><span class="token punctuation">(</span>matcher<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>matcher<span class="token punctuation">.</span><span class="token function">group</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>



    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="Pattern对象"><a href="#Pattern对象" class="headerlink" title="Pattern对象"></a>Pattern对象</h5><p>  pattern对象是一个正则表达式对象，pattern类没有公共构造方法，要创建一个pattern对象。需要调用公共的静态方法，返回一个pattern对象，该方法接收一个正则表达式作为它的第一个参数。</p>
<p>  Pattern.compile(正则表达式);  //返回一个Pattern对象区分大小写</p>
<p>  Pattern.compile(正则表达式，Pattern.CASE_INSENSITIVE);  //返回一个Pattern对象，不区分大小写</p>
<p>  Pattern.matches(正则表达式,匹配字符串);   //如果==整体匹配==成功返回true，否则false</p>
<ul>
<li>Pattern.matches(regstr,content);方法底层是封装了创建Matcher类的Pattern，再创建Matcher对象，调用matcher方法。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">boolean</span> <span class="token function">matches</span><span class="token punctuation">(</span><span class="token class-name">String</span> regex<span class="token punctuation">,</span> <span class="token class-name">CharSequence</span> input<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
<span class="token class-name">Pattern</span> p <span class="token operator">=</span> <span class="token class-name">Pattern</span><span class="token punctuation">.</span><span class="token function">compile</span><span class="token punctuation">(</span>regex<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Matcher</span> m <span class="token operator">=</span> p<span class="token punctuation">.</span><span class="token function">matcher</span><span class="token punctuation">(</span>input<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> m<span class="token punctuation">.</span><span class="token function">matches</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//判断字符串是否符合指定正则表达式</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> content <span class="token operator">=</span> <span class="token string">"ihwiuadodoubaodfaww"</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> regstr <span class="token operator">=</span> <span class="token string">".*douba\\w*"</span><span class="token punctuation">;</span>
        <span class="token keyword">boolean</span> matches <span class="token operator">=</span> <span class="token class-name">Pattern</span><span class="token punctuation">.</span><span class="token function">matches</span><span class="token punctuation">(</span>regstr<span class="token punctuation">,</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>matches<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>











<h5 id="Matcher对象"><a href="#Matcher对象" class="headerlink" title="Matcher对象"></a>Matcher对象</h5><p>  matcher对象是对输入字符串进行解释和匹配的引擎，和pattern类一样，matcher也没有公共构造方法，你需要调用pattern对象的matcher方法来获得一个matcher对象。</p>
<p>  Matcher对象.find()</p>
<ol>
<li>如果找到返回true，未找到返回false，多次执行自动迭代。</li>
<li>根据相应的正则表达式规则，匹配指定的字符串。</li>
<li>matcher对象的oldList属性记录为此次找到子字符串的结束索引+1，当下次开始寻找时，会从oldList记录的索引开始寻找。(自动迭代)</li>
<li>找到对应子字符串后，将开始索引记录到matcher对象的int [] groups属性中。使用matcher.group(0)获得整体子字符串。<ul>
<li>groups[0] = 子字符串的开始索引</li>
<li>groups[1] = 子字符串的结束索引 + 1</li>
</ul>
</li>
<li>如果正则表达式中使用()进行分组，会将分组得到的部分子字符串储存到matcher对象的int [] groups属性中，使用索引2，3进行储存在查询字符串的索引。如果有更多分组以此类推。<ul>
<li>groups[3] = 分组的开始索引</li>
<li>groups[4] = 分组的结束索引 + 1</li>
</ul>
</li>
</ol>
<p>  Matcher对象.group(index)</p>
<ul>
<li>index为0时返回此次寻找到的子字符串整体。</li>
<li>index大于0时，返回指定的第几个分组返回此次找到的部分子字符串。</li>
</ul>
<table>
<thead>
<tr>
<th>方法</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>public int start()</td>
<td>返回以前匹配的初始索引</td>
</tr>
<tr>
<td>public int start(int group)</td>
<td>返回再以前的匹配操作期间，由给定组所捕获的子序列的初始索引</td>
</tr>
<tr>
<td>public int end()</td>
<td>返回最后匹配字符之后的偏移量</td>
</tr>
<tr>
<td>public int end(int group)</td>
<td>返回再以前的匹配操作期间，由给定组所捕获的子序列的最后索引</td>
</tr>
<tr>
<td>public boolean lookingAt()</td>
<td>尝试将从区域开头开始的输入序列于该模式匹配</td>
</tr>
<tr>
<td>public boolean find()</td>
<td>尝试查找和该模式匹配的输入序列的下一个子序列</td>
</tr>
<tr>
<td>public boolean find(int start)</td>
<td>重置此匹配器，然后尝试查找匹配该模式，从指定索引开始的的输入序列的的下一个子序列</td>
</tr>
<tr>
<td>public boolean marches()</td>
<td>尝试将整个区域与模式匹配</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>方法</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>public Mather appendReplacement(StringBuffer sb,String replacement)</td>
<td>实现非终端添加和替换步骤</td>
</tr>
<tr>
<td>public StringBuffer appendTail(StringBuffer sb)</td>
<td>实现终端添加和替换步骤</td>
</tr>
<tr>
<td>public String replaceAll(String replacement)</td>
<td>替换模式和给定替换字符串匹配的输入序列的每一个子序列</td>
</tr>
<tr>
<td>public String replaceFirst(String replacement)</td>
<td>替换模式与给定替换字符串匹配的输入序列的第一个子序列</td>
</tr>
<tr>
<td>public static String quoteReplacement(String s)</td>
<td>返回指定字符串的字面替换字符串</td>
</tr>
<tr>
<td>public Matcher appendReplacement(StringBuffer sb，String replacement)</td>
<td>实现非终端添加和替换</td>
</tr>
<tr>
<td>public StringBuffer appendTail(StringBuffer sb)</td>
<td>实现终端添加和替换步骤</td>
</tr>
</tbody></table>
<p>全部替换</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> content <span class="token operator">=</span> <span class="token string">"ihwiuadodoubaodfaww"</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> regstr <span class="token operator">=</span> <span class="token string">"douba"</span><span class="token punctuation">;</span>

        <span class="token class-name">Pattern</span> pattern <span class="token operator">=</span> <span class="token class-name">Pattern</span><span class="token punctuation">.</span><span class="token function">compile</span><span class="token punctuation">(</span>regstr<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">Matcher</span> matcher <span class="token operator">=</span> pattern<span class="token punctuation">.</span><span class="token function">matcher</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//不会修改原来的字符串，而是新创建一个字符串</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span> matcher<span class="token punctuation">.</span><span class="token function">replaceAll</span><span class="token punctuation">(</span><span class="token string">"替换字符串"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>











<h5 id="PatternSyntaxException"><a href="#PatternSyntaxException" class="headerlink" title="PatternSyntaxException"></a>PatternSyntaxException</h5><p>  patternSyntaxException 是一个非强制异常类，它表示一个正则表达式模式中的语法错误。</p>
<h5 id="正则表达式的语法："><a href="#正则表达式的语法：" class="headerlink" title="正则表达式的语法："></a>正则表达式的语法：</h5><p>  正则表达式中有各种不同功能的元字符：</p>
<ul>
<li>限定符</li>
<li>选择匹配符号</li>
<li>分组组合和反向引用符</li>
<li>字符匹配符</li>
<li>定位符</li>
</ul>
<h6 id="元字符-转义字符"><a href="#元字符-转义字符" class="headerlink" title="元字符-转义字符  \\"></a>元字符-转义字符  \\</h6><p>  在使用正则表达式检索某些特殊字符的时候，需要使用到转移符号，否则检索不到结果，甚至会报错。</p>
<p>  ==java的正则表达式中两个反斜杠代表其他语言中的一个反斜杠==</p>
<h6 id="元字符-字符匹配符号"><a href="#元字符-字符匹配符号" class="headerlink" title="元字符-字符匹配符号"></a>元字符-字符匹配符号</h6><table>
<thead>
<tr>
<th>符号</th>
<th>含义</th>
<th>实例</th>
<th>解释</th>
</tr>
</thead>
<tbody><tr>
<td>[ ]</td>
<td>可以接收的字符列表</td>
<td>[abcd]</td>
<td>a,b,c,d中的任意一个字符</td>
</tr>
<tr>
<td>[ ^ ]</td>
<td>不接受的字符列表</td>
<td>[^abcd]</td>
<td>除a,b,c,d中的字符包含数字和特殊符号</td>
</tr>
<tr>
<td>-</td>
<td>连字符</td>
<td>A-Z</td>
<td>任意单个大写字母</td>
</tr>
<tr>
<td>.</td>
<td>匹配除了\n之外的任意字符</td>
<td>a..b</td>
<td>以a开头b结尾中间包含任意的两个字符的长度为4的字符串</td>
</tr>
<tr>
<td>\\d</td>
<td>匹配单个数字字符 [0-9]</td>
<td>\\d{3}</td>
<td>包含三个数字的字符串</td>
</tr>
<tr>
<td>\\D</td>
<td>匹配单个非数字字符 [^ 0-9 ]</td>
<td>\\D{3}</td>
<td>包含三个非数组的字符串</td>
</tr>
<tr>
<td>\\w</td>
<td>匹配一个数字，大小写字母字符 [0-9a-zA-Z]下划线</td>
<td>\\w{3}</td>
<td>匹配三个数字或者大小写字母组成的字符串</td>
</tr>
<tr>
<td>\\W</td>
<td>匹配非数字，大小写字母字符和下划线 [^0-9a-zA-Z]</td>
<td>\\W{3}</td>
<td>匹配非三个数字或者非大小写字母组成的字符串</td>
</tr>
<tr>
<td>\\s</td>
<td>匹配任何空白字符(空格制表符等)</td>
<td></td>
<td></td>
</tr>
<tr>
<td>\\S</td>
<td>匹配任何非空白字符</td>
<td></td>
<td></td>
</tr>
</tbody></table>
<ul>
<li>==在[] 中括号中的字符不需要转义直接使用==</li>
<li>使用 (?i) 表示后方的字符大小写不敏感<ul>
<li>(?i) abcd     匹配abcd且大小写不敏感</li>
<li>ab(?i) cd     cd大小写不敏感</li>
<li>ab((?i) c)d     c大小写不敏感</li>
</ul>
</li>
</ul>
<h6 id="元字符-选择匹配符"><a href="#元字符-选择匹配符" class="headerlink" title="元字符-选择匹配符"></a>元字符-选择匹配符</h6><p>  在匹配某个字符串的时候具有选择性，即：既可以匹配这个，又可以匹配另一个，可以使用选择匹配符  | 。</p>
<p>   |  匹配之前或者之后的表达式，成立一边既可以匹配</p>
<ul>
<li>ab|cd     匹配ab或者cd</li>
</ul>
<h6 id="元字符-限定符"><a href="#元字符-限定符" class="headerlink" title="元字符-限定符"></a>元字符-限定符</h6><p>  用于只当前面的字符和组合项连续出现多少次。</p>
<p>  java的默认匹配机制为贪婪匹配，即尽可能匹配多的。</p>
<table>
<thead>
<tr>
<th>符号</th>
<th>含义</th>
<th>实例</th>
<th>说明</th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>*</td>
<td>提示字符重复0到多次</td>
<td>(abc)*</td>
<td>仅包含任意个abc的字符串</td>
<td></td>
</tr>
<tr>
<td>+</td>
<td>提示字符重复1到多次</td>
<td>(abc)+</td>
<td>仅包含1到多个abc的字符串</td>
<td></td>
</tr>
<tr>
<td>?</td>
<td>提示字符重复0到1次</td>
<td>abc?</td>
<td>ab或者abc</td>
<td></td>
</tr>
<tr>
<td>{n}</td>
<td>提示字符重复的次数</td>
<td>[abc]{3}</td>
<td>以abc其中字母组成的长度为3的字符串</td>
<td></td>
</tr>
<tr>
<td>{n,}</td>
<td>至少n次匹配</td>
<td>a{3,}</td>
<td>三个a或者更多组成的字符串</td>
<td></td>
</tr>
<tr>
<td>{n,m}</td>
<td>至少n次匹配但是不多于m次匹配</td>
<td>a{3,6}</td>
<td>三个到六个a组成的字符串</td>
<td></td>
</tr>
</tbody></table>
<ul>
<li>？ ==当此字符紧随任何其他限定符时==，匹配模式时非贪心匹配，匹配尽可能短的字符串。默认为贪心模式，匹配尽可能长的字符串。<ul>
<li>o+   //尽可能长的匹配o的个数</li>
<li>o+?   //只会匹配一个o</li>
</ul>
</li>
</ul>
<h6 id="元字符-定位符"><a href="#元字符-定位符" class="headerlink" title="元字符-定位符"></a>元字符-定位符</h6><p>  定位符，规定要匹配的字符出现的位置，比如在字符串开始还是结束的位置。</p>
<table>
<thead>
<tr>
<th>符号</th>
<th>含义</th>
<th>示例</th>
<th>说明</th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>^</td>
<td>指定起始字符</td>
<td>^[0-9]+[a-z]*</td>
<td>以至少一个数字开头,任意个小写字母组成的字符串</td>
<td></td>
</tr>
<tr>
<td>$</td>
<td>指定结束字符</td>
<td>[a-z]$</td>
<td>以小写字母结尾的字符串</td>
<td></td>
</tr>
<tr>
<td>\\b</td>
<td>匹配目标字符串的边界</td>
<td>dou\\b</td>
<td>字符串的边界指的是字串间有空格，或者目标字符串的结束位置</td>
<td>baodou</td>
</tr>
<tr>
<td>\\B</td>
<td>匹配目标字符串的非边界</td>
<td>dou\\B</td>
<td>匹配到非边界的指定字符</td>
<td>doubao</td>
</tr>
</tbody></table>
<p>分组</p>
<ul>
<li>(pattern)  非命名捕获，捕获匹配的子字符串，编号为0的第一个捕获时由整个正则表达式模式匹配的文本，其他捕获结构则根据左括号的顺序从1开始自动编号。</li>
<li>(?&lt;name pattern)  命名捕获。将匹配的子字符串捕获到一个组名或是编号名称中，用于那么的字符串 不能包含任何标点符号，并且不能是以数字开头可以使用单引号代替尖括号 (?’name’)</li>
</ul>
<p>特殊分组(非捕获)</p>
<ul>
<li>(?:pattern)  匹配pattern但不捕获该匹配的子表达式，即它是一个非捕获匹配，不存储以后使用的匹配，<ul>
<li><code>regstr = &quot; plan(?:t|e) &quot;</code>  //捕获 plane或者plant</li>
</ul>
</li>
<li>(?=pattern)  是一个非捕获匹配。<ul>
<li><code>regstr = &quot; plan(?=t|e) &quot; </code>    //捕获plane或者plant前的plan</li>
</ul>
</li>
<li>(?!pattern) 该表达式匹配不处于pattern的字符串的起始点的搜索字符串，他是一个非捕获匹配。<ul>
<li><code>regstr = &quot; plan(?!t|e) &quot;  </code>   //bh不会捕获plane或者plant前的plan，而是捕获其他的plan</li>
</ul>
</li>
</ul>
<h5 id="反向引用"><a href="#反向引用" class="headerlink" title="反向引用"></a>反向引用</h5><p>  圆括号的内容被捕获后，可以在这个括号后被使用，从而写出一个比较使用的匹配模式，这种引用即可以实在正则表达式内部，也可以是在正则表达式外部，内部反向引用==\\分组号==，外部反向引用==$分组号==</p>
<ul>
<li>String restr = ”(\\d)\\1“；</li>
<li>matcher.replaceAll(“$1”);</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span><span class="token string">"开始的.开开.始.始，我..我..我..我..们们都是孩子子"</span><span class="token punctuation">;</span>

        <span class="token class-name">String</span> restr1 <span class="token operator">=</span> <span class="token string">"\\."</span><span class="token punctuation">;</span>
        <span class="token class-name">Pattern</span> compile <span class="token operator">=</span> <span class="token class-name">Pattern</span><span class="token punctuation">.</span><span class="token function">compile</span><span class="token punctuation">(</span>restr1<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">Matcher</span> matcher <span class="token operator">=</span> compile<span class="token punctuation">.</span><span class="token function">matcher</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
  		<span class="token comment">//取出符号点</span>
        <span class="token class-name">String</span> str1 <span class="token operator">=</span> matcher<span class="token punctuation">.</span><span class="token function">replaceAll</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str1<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">String</span> restr2 <span class="token operator">=</span> <span class="token string">"(.)\\1+"</span><span class="token punctuation">;</span>

        compile <span class="token operator">=</span> <span class="token class-name">Pattern</span><span class="token punctuation">.</span><span class="token function">compile</span><span class="token punctuation">(</span>restr2<span class="token punctuation">)</span><span class="token punctuation">;</span>

        matcher <span class="token operator">=</span> compile<span class="token punctuation">.</span><span class="token function">matcher</span><span class="token punctuation">(</span>str1<span class="token punctuation">)</span><span class="token punctuation">;</span>
  		<span class="token comment">//使用第一个符号替换重复的所有符号</span>
        <span class="token class-name">String</span> str2 <span class="token operator">=</span> matcher<span class="token punctuation">.</span><span class="token function">replaceAll</span><span class="token punctuation">(</span><span class="token string">"$1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h6 id="String类中的正则表达式"><a href="#String类中的正则表达式" class="headerlink" title="String类中的正则表达式"></a>String类中的正则表达式</h6><p>  public String replaceAll(正则表达式,字符串);</p>
<p>  //使用指定值替换符合正则表达式的字符</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"开始的开始,我们都是孩子。最后的最后,都将称为天使。"</span><span class="token punctuation">;</span>
	<span class="token comment">//使用""替换"，"和"。"</span>
	<span class="token class-name">String</span> replace <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">replaceAll</span><span class="token punctuation">(</span><span class="token string">"。|,"</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>replace<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>  public boolean matches(正则表达式)；</p>
<p>  //验证一个字符串是否完全符合一个正则表达式（整体匹配）</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"开始的开始,我们都是孩子。最后的最后,都将称为天使。"</span><span class="token punctuation">;</span>
    <span class="token keyword">boolean</span> matches <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">matches</span><span class="token punctuation">(</span><span class="token string">"^(开始).*\\。"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>matches<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>  public  String[] split(正则表达式)；</p>
<p>  //以符合正则表达式的子字符串分割字符串</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
      <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"开始的开始,我们都是孩子。最后的最后,都将称为天使。"</span><span class="token punctuation">;</span>

  <span class="token comment">//以指定正则表达式分割字符串</span>
      <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> split <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">",|。"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> s<span class="token operator">:</span>split<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
          <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span>


  <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>






























































































































































<h2 id="二-模式"><a href="#二-模式" class="headerlink" title="二. 模式"></a>二. 模式</h2><h3 id="单例模式"><a href="#单例模式" class="headerlink" title="单例模式"></a>单例模式</h3><p>  单例设计模式，就是采取一定的方法保证在整个软件系统中，对某个类只能 存在一个对象实例，并且该类只提供一个取得其对象实例的方法。</p>
<p>  单例模式：</p>
<ol>
<li>饿汉式<ol>
<li>饿汉式可能创建对象却不使用。可能造成资源浪费</li>
</ol>
</li>
</ol>
<ul>
<li>构造器私有化   —- 防止new创建对象</li>
</ul>
<ul>
<li>类的内部创建对象</li>
<li>向外暴露一个静态的公共方法，返回对象</li>
<li>代码实现</li>
</ul>
<ol start="2">
<li>懒汉式</li>
</ol>
<ul>
<li>构造器私有化    —–防止new创建对象</li>
<li>类的内部创建对象</li>
<li>向外暴露一个静态的公共方法</li>
<li>代码实现</li>
</ul>
<p>  总结：</p>
<ul>
<li><p>二者最主要的区别就是创建对象的时机不同，饿汉式是在类加载就创建了对象实例，而懒汉式是在使用时才创建。</p>
</li>
<li><p>饿汉式不存在线程安全问题，懒汉式存在线程安全问题。</p>
</li>
<li><p>懒汉式存在浪费资源的可能。</p>
</li>
</ul>
<h3 id="模板设计模式"><a href="#模板设计模式" class="headerlink" title="模板设计模式"></a>模板设计模式</h3><p>  使用抽象类：</p>
  <pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">Work</span><span class="token punctuation">&#123;</span>
  <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token keyword">do</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token function">job</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    其他代码<span class="token operator">/</span>共用代码；
  <span class="token punctuation">&#125;</span>
  <span class="token keyword">abstract</span> <span class="token keyword">void</span> <span class="token function">job</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        Author:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">凡诚</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        Link:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://example.com/2022/03/11/java-jin-jie/">http://example.com/2022/03/11/java-jin-jie/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        Reprint policy:
                    </i>
                </span>
                <span class="reprint-info">
                    All articles in this blog are used except for special statements
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    reprint polocy. If reproduced, please indicate source
                    <a href="/about" target="_blank">凡诚</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>Copied successfully, please follow the reprint policy of this article</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">more</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/java/">
                                    <span class="chip bg-color">java</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="MTAyMC81NzU4NC8zNDA0OA==">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;Previous</div>
            <div class="card">
                <a href="/2022/03/11/wei-xin-xiao-cheng-xu/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/2.jpg" class="responsive-img" alt="微信小程序基础">
                        
                        <span class="card-title">微信小程序基础</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            微信小程序，小程序的一种，英文名Wechat Mini Program，是一种不需要下载安装即可使用的应用，它实现了应用“触手可及”的梦想，用户扫一扫或搜一下即可打开应用。
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-03-11
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/" class="post-category">
                                    微信小程序
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/">
                        <span class="chip bg-color">微信小程序</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                Next&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2022/03/08/linux-cao-zuo-xi-tong/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/8.jpg" class="responsive-img" alt="linux基础">
                        
                        <span class="card-title">linux基础</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Linux是一个领先的操作系统,世界上运算最快的10台超级计算机运行的都是Linux操作系统。
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-03-08
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" class="post-category">
                                    操作系统
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/linux/">
                        <span class="chip bg-color">linux</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;TOC</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        font-size: 15px;
        color: blue;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="2477330090"
                   fixed='true'
                   autoplay='false'
                   theme='blue'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2022</span>
            
            <span id="year">2022</span>
            <a href="/about" target="_blank">凡诚</a>
            &nbsp;&nbsp;<a href="https://hexo.io/" target="_blank"></a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">607.1k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2022";
                    var startMonth = "11";
                    var startDate = "7";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/fanshicheng" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2639144944@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2639144944" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2639144944" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;Search</span>
            <input type="search" id="searchInput" name="s" placeholder="Please enter a search keyword"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>

    <script type="text/javascript">
        var OriginTitile=document.title,st;
        document.addEventListener("visibilitychange",function(){
            document.hidden?(document.title="(Ő∀Ő3)ノ",clearTimeout(st)):(document.title="ヽ(●-`Д´-)ノ欢迎回来",st=setTimeout(function(){document.title=OriginTitile},3e3))
        })
    </script>
    <!-- <script src="/js/cursor.js"></script> -->
    <script type="text/javascript">
        //只在桌面版网页启用特效
        var windowWidth = $(window).width();
        if (windowWidth > 768) {
            document.write('<script type="text/javascript" src="/js/sakura.js"><\/script>');
        }
        </script>
    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    
    <script>
        (function (i, s, o, g, r, a, m) {
            i["DaoVoiceObject"] = r;
            i[r] = i[r] || function () {
                (i[r].q = i[r].q || []).push(arguments)
            }, i[r].l = 1 * new Date();
            a = s.createElement(o), m = s.getElementsByTagName(o)[0];
            a.async = 1;
            a.src = g;
            a.charset = "utf-8";
            m.parentNode.insertBefore(a, m)
        })(window, document, "script", ('https:' == document.location.protocol ? 'https:' : 'http:') +
            "//widget.daovoice.io/widget/6984b559.js", "daovoice")
        daovoice('init', {
            app_id: "377cb7de"
        });
        daovoice('update');
    </script>
    

	
    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.7'
        zIndex="-1" count="99"
        src="/libs/background/canvas-nest.js"></script>
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    
    <!-- 冒泡 -->
    
<canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
<script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
<script type="text/javascript" src="/js/fireworks.js"></script>


</body>

</html>
